How can I turn off the normal behavior input feed on a text input and replace it with my function?

I tried to fix some of the fixes found in stackoverflow and elsewhere, but I could not get them to work properly. They either turn off the input key everywhere, or simply do not work at all (or they are not explained properly).

I need a normal view to enter an input key to work with all other elements except this one text input, and to replace it with my own function when text input is selected.

+5
source share
3 answers

How to get pressedEnter ?

$('input.the-one-text-input').keydown(function(e) {
  if(e.keyCode == 13) { // enter key was pressed
    // run own code
    return false; // prevent execution of rest of the script + event propagation / event bubbling + prevent default behaviour
  }
});

Also pay attention to this comment on this page:

** - Google ( ), , "keyup" "keypress" Firefox, IE Chrome. "keypress", -, Firefox.

100% , Chrome. , IE.

+9

http://jsfiddle.net/yzfm9/9/

, .

HTML

<form id="nya">
    username <input type="text" id="input_username" /><br/>
    email <input type="text" id="input_email" /><br/>
    hobby <input type="text" id="input_hobby" /><br/>
    <input type="submit" />
</form>

JS

$('#nya').submit(function() {
    var focusedId = ($("*:focus").attr("id"));
    if(focusedId == 'input_email') {
       // do your custom stuff here

       return false;
    }
});
+3

jQuery.keypress(), , .

HTML

<form method="post" action="">
    <input type="text" name="submit1" id="submit1" />
    <input type="text" name="noSubmit1" id="noSubmit1" />
    <input type="text" name="submit2" id="submit2" />
    <input type="submit" />
</form>

JQuery

​$('#noSubmit1​​​​​​​​​​​​​​​').keypress(function(event) {
    if (event.which == 13 ) {
        event.preventDefault();
    }
});​​

, , . (13), ( ). : http://jsfiddle.net/cchana/UrHz7/2/

-1

All Articles