I'm developing an application that runs on one page, and does not reload. I have a form wit开发者_JS百科h only an input type text, no submit button. I included the onchange event, after filling the textbox with data, I want to execute the function bound to the onchange event when I press enter, but the form is rather submitted and [it attempts to load a new page]. Please what do I do? Thanks
You can hook the keypress
event on the text box and call your handler, and cancel the event to prevent the form submission:
$("selector_for_your_text_box").keypress(function(event) {
if (event.which === 13) {
// call your `change` logic here
// Cancel the event
return false;
}
});
Live example
This should work:
$(function() {
$('#yourInput').keypress(function(event) {
var key = event.keyCode || event.which;
if (key == 13) {
event.preventDefault();
// call your function here
});
});
Yuo can bind to the keypress
event on the input:
$('#inputfieldid').keypress(function() {
alert('Handler for .keypress() called.');
});
Try the demo link http://jsfiddle.net/HDkJW/
精彩评论