Submitting a form on 'Enter' with jQuery?

后端 未结 14 1635
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 14:45

I have a bog-standard login form - an email text field, a password field and a submit button on an AIR project that\'s using HTML/jQuery. When I hit Enter on the form, the

相关标签:
14条回答
  • 2020-11-22 15:28
    $('.input').keypress(function (e) {
      if (e.which == 13) {
        $('form#login').submit();
        return false;    //<---- Add this line
      }
    });
    

    Check out this stackoverflow answer: event.preventDefault() vs. return false

    Essentially, "return false" is the same as calling e.preventDefault and e.stopPropagation().

    0 讨论(0)
  • 2020-11-22 15:29

    Try this:

    var form = document.formname;
    
    if($(form).length > 0)
    {
        $(form).keypress(function (e){
            code = e.keyCode ? e.keyCode : e.which;
            if(code.toString() == 13) 
            {
                 formsubmit();
            }
        })
    }
    
    0 讨论(0)
  • 2020-11-22 15:30

    You can also simply add onsubmit="return false" to the form code in the page to prevent the default behaviour.

    Then hook (.bind or .live) the form's submit event to any function with jQuery in the javascript file.

    Here's a sample code to help:

    HTML

    <form id="search_form" onsubmit="return false">
       <input type="text" id="search_field"/>
       <input type="button" id="search_btn" value="SEARCH"/>
    </form>
    

    Javascript + jQuery

    $(document).ready(function() {
    
        $('#search_form').live("submit", function() {
            any_function()
        });
    });
    

    This is working as of 2011-04-13, with Firefox 4.0 and jQuery 1.4.3

    0 讨论(0)
  • 2020-11-22 15:32

    Here's a way to do this as a JQuery plugin (in case you want to re-use the functionality):

    $.fn.onEnterKey =
        function( closure ) {
            $(this).keypress(
                function( event ) {
                    var code = event.keyCode ? event.keyCode : event.which;
    
                    if (code == 13) {
                        closure();
                        return false;
                    }
                } );
        }
    

    Now if you want to decorate an <input> element with this type of functionality it's as simple as this:

    $('#your-input-id').onEnterKey(
        function() {
            // Do stuff here
        } );
    
    0 讨论(0)
  • 2020-11-22 15:34

    In HTML codes:

    <form action="POST" onsubmit="ajax_submit();return false;">
        <b>First Name:</b> <input type="text" name="firstname" id="firstname">
        <br>
        <b>Last Name:</b> <input type="text" name="lastname" id="lastname">
        <br>
        <input type="submit" name="send" onclick="ajax_submit();">
    </form>
    

    In Js codes:

    function ajax_submit()
    {
        $.ajax({
            url: "submit.php",
            type: "POST",
            data: {
                firstname: $("#firstname").val(),
                lastname: $("#lastname").val()
            },
            dataType: "JSON",
            success: function (jsonStr) {
                // another codes when result is success
            }
        });
    }
    
    0 讨论(0)
  • 2020-11-22 15:35

    In addition to return false as Jason Cohen mentioned. You may have to also preventDefault

    e.preventDefault();
    
    0 讨论(0)
提交回复
热议问题