最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Trigger a button click on the Enter key in a text box misses its value - Stack Overflow

programmeradmin4浏览0评论

I have

Html:

<form  action="" method="post"  id="contactForm">
    <input id='inputbox' name="inputbox" type="text" />
    <button type="button" id='search'> search </button>
</form>

JavaScript

$(document).ready(function() {
    $("#inputbox").keyup(function(event){
        if(event.keyCode == 13){
            $("#search").click();
        }
    });

    $('#search').click(function(){
         var inputbox= $("#inputbox").val();  
        //stuff
    });
});        

inputbox value is nothing when I press enter, however if I click on button It works perfectly with same input value

maybe making inputbox global?

I have

Html:

<form  action="" method="post"  id="contactForm">
    <input id='inputbox' name="inputbox" type="text" />
    <button type="button" id='search'> search </button>
</form>

JavaScript

$(document).ready(function() {
    $("#inputbox").keyup(function(event){
        if(event.keyCode == 13){
            $("#search").click();
        }
    });

    $('#search').click(function(){
         var inputbox= $("#inputbox").val();  
        //stuff
    });
});        

inputbox value is nothing when I press enter, however if I click on button It works perfectly with same input value

maybe making inputbox global?

Share Improve this question edited Mar 29, 2013 at 19:27 Default 16.5k3 gold badges28 silver badges38 bronze badges asked Mar 29, 2013 at 19:17 edgarmtzeedgarmtze 25.1k81 gold badges248 silver badges392 bronze badges 6
  • Did you try event.which instead of event.keyCode ? Did you prevent the form from submitting! – adeneo Commented Mar 29, 2013 at 19:18
  • 4 Try $("#search").trigger("click"); – Oliver Tappin Commented Mar 29, 2013 at 19:19
  • @adeneo - that won't matter, jQuery has that built in. – Oliver Tappin Commented Mar 29, 2013 at 19:19
  • What about $('#contactForm').submit(); – TJ Fogarty Commented Mar 29, 2013 at 19:20
  • @Titanium - Nope, jQuery normalizes event.which, but not keyCode. – adeneo Commented Mar 29, 2013 at 19:28
 |  Show 1 more ment

1 Answer 1

Reset to default 8

The problem is that the enter key defaults to submitting your form, even without a submit button. So you should block the submission by changing the event binding to keypress and using event.preventDefault(), like this:

$("#inputbox").keypress(function(event){
    if(event.keyCode == 13){
        event.preventDefault();
        $("#search").click();
    }
});

Alternatively, you could use .submit() to trigger your function, change the input type to submit, and avoid separate handling for keys and clicks.

HTML:

<form action="" method="post" id="contactForm">
    <input id='inputbox' name="inputbox" type="text" />
    <input type="submit" value="Search">
</form>

JavaScript:

$(document).ready(function() {
    $("#contactForm").submit(submitSearch);
}); 

function submitSearch(event) {
     event.preventDefault();

     //do other stuff
     alert($("#inputbox").val());
}
发布评论

评论列表(0)

  1. 暂无评论