Stop reloading page with 'Enter Key'

前端 未结 9 896
臣服心动
臣服心动 2020-12-25 10:11

I have a search box at the top of page that makes an ajax call when a user hits the adjacent button. I am trying to update the input tag so that when a user hit the \'en

相关标签:
9条回答
  • 2020-12-25 11:07

    Don't bind to the inputs; bind to the form. Assuming the form has an ID of searchForm:

    $("#searchForm").submit(function() {
        search($("#searchText").get(0));
        return false;
    });
    

    Try it out.

    It can also be done with plain JavaScript:

    document.getElementById('searchForm').addEventListener('submit', function(e) {
        search(document.getElementById('searchText'));
        e.preventDefault();
    }, false);
    
    0 讨论(0)
  • 2020-12-25 11:17

    Does your JS execute immediately or on document ready? If it's not in a document ready the button won't exist at the time you're trying to call bind.

    0 讨论(0)
  • 2020-12-25 11:18

    This is what ended up working for me. Please note that my struggle was to find the object that triggered the form submit:

    $('#missingStaff').submit(function (e) {
    e.preventDefault();
    var comment = $(document.activeElement)[0];
    submitComments(comment);
    

    });

    0 讨论(0)
提交回复
热议问题