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

javascript - IE8 - Enter key won't submit my search field - Stack Overflow

programmeradmin4浏览0评论

Hey guys, I have a search field that is not submitting when the enter key is hit, this issue is only happening on IE8, every other browser is working just fine (even IE6). Please guys I need a hand with his, find below the code I have for it.

<div class="box-search">
                <input type="text" class="text-box" id="text-search" />
                <label class="overlabel" for="text-search">Enter keyword(s)</label>
                <input type="submit" value="" name="btn-submit" class="btn-go" onclick="javascript:goSearch();return false;" />
</div>

Ok I forgot to mention this form is in a ASP coded page, that's why it is not wrapped inside the form element.

Hey guys, I have a search field that is not submitting when the enter key is hit, this issue is only happening on IE8, every other browser is working just fine (even IE6). Please guys I need a hand with his, find below the code I have for it.

<div class="box-search">
                <input type="text" class="text-box" id="text-search" />
                <label class="overlabel" for="text-search">Enter keyword(s)</label>
                <input type="submit" value="" name="btn-submit" class="btn-go" onclick="javascript:goSearch();return false;" />
</div>

Ok I forgot to mention this form is in a ASP coded page, that's why it is not wrapped inside the form element.

Share Improve this question edited Jul 30, 2010 at 13:59 Ozzy asked Jul 30, 2010 at 13:51 OzzyOzzy 9153 gold badges12 silver badges17 bronze badges 1
  • 1 You can go ahead and remove javascript: from the onclick handler, it's pletely useless there. – Andy E Commented Jul 30, 2010 at 14:10
Add a ment  | 

6 Answers 6

Reset to default 4

You need to put some <form></form> tags around the textbox and button. Like so

<form method='POST' onsubmit='javascript:goSearch();return false;'> 
            <input type="text" class="text-box" id="text-search" />       
            <label class="overlabel" for="text-search">Enter keyword(s)</label>       
            <input type="button" value="" name="btn-submit" class="btn-go" onclick="javascript:goSearch();return false;" />
</form>

Another way would be to use the keydown event on the textbox and check whether it was the enter key.

Hope this helps.

I have found there to be a bug in IE8 and sometimes a form won't submit on enter key.

The best way would be to set an event to handle enter being pressed.

In jQuery you would do:

$("input_box_id").keydown(function(e){

     if (e.keyCode == 13) //enter
     {
         $("btn-submit").click();
     }
});

In JavaScript it would be:

document.getElementById("input_box_id").onclick = function(e){

   var keycode =(window.event) ? event.keyCode : e.keyCode;

   if (keycode == 13) //enter
   {
      document.getElementById("input_box_id").click();
   }

};

And change Html to:

<form action="url_here" method="post">
<div class="box-search">
    <input type="text" class="text-box" id="text-search" />
    <label class="overlabel" for="text-search">Enter keyword(s)</label>
    <input type="submit" value="" id="btn-submit" name="btn-submit" class="btn-go" />
</div>
</form>

Ignore the form tags if you've already got a Asp form.

Another way, instead of the onclick on the submit button, would be to do this.

<form action="script.php" method="post" onsubmit="goSearch();return false">
<div class="box-search">
    <input type="text" class="text-box" id="text-search" />
    <label class="overlabel" for="text-search">Enter keyword(s)</label>
    <input type="submit" value="" name="btn-submit" class="btn-go" />
</div>
</form>

Edit: Added action and method attributes. The action attribute is required for validation.

Would it be anything to do with the fact you have an onclick event with a function call to GoSearch and a return false attached to a 'submit' input type?

Can you past the contents of the goSearch() function?

What happens if you remove the "return false;" from the event handler for the submit? When a user hits ENTER in a text input field, IE behaves as if the submit button had been used but the "false" prevents the event bubbling.

EDIT: with new ASP information.

See this: http://mikepope./blog/AddComment.aspx?blogid=309 He has an explanation of how it works so no details here, except to say that all you need to do is add the following to your page_load event:

Page.RegisterHiddenField("__EVENTTARGET", "MyDefaultButton");

Old ticket, but I'd like to chime in: IE8 does the following peculiar thing: the Enter key will submit the form, but any

<input type="submit" name="MySubmitButton" value="I hope I detect THIS VALUE in POST" />

won't be sent in the POST.

IE9 changes the behavior and sends the value. Chrome has always sent the value, as far as my tests have shown.

There are a ton of "Enter not submitting in IE8" plaints out there, and I feel lots of them can be contributed to this behavior. I hope this helps some of them.

发布评论

评论列表(0)

  1. 暂无评论