javascript - Controlling ENTER key -
how can make form in page doesn't submit on pressing enter — rather. same work pressing particular button or icon or image?
here contents of form:
<input type="text" id="txt" /><input type="button" value="search" onclick="searchresult()" /> the problem if press enter, form submits , text field clears function searchresult() doesn't show effect. when pressing button, works well.
html
<input type="text" id="txt"/> <input type="button" value="search"/> jquery
$('input[type=text]').on('keyup', function(e) { if(e.which == 13) { // 13 keycode enter e.preventdefault(); } }) you can bind submit() following
$('form').submit(function(e) { // instead of `form`, // use `id` or `class` combination if(e.which == 13) { e.preventdefault(); } }); remainder
don't forget place code within
$(document).ready(function() { // code }); in short
$(function() { // code });
Comments
Post a Comment