IE needs 2 textboxes to submit a button?

2019-02-24 23:15发布

I've got this weird problem, and I'm not sure whether ASP.NET or IE 7 is to blame...

The idea is this: When I have just one textbox and one (submit) button on my form, then pressing ENTER while inside the textbox will just POST the textbox'es value. The button does not get submitted and does not trigger the Click even server-side.

When I have two textboxes, then hitting ENTER in any of them will also send the button and trigger the Click event.

WTF?


Added: Seems to be an IE-related issue. Here's a simple example to demonstrate. Pay attention to the address bar and hit ENTER when in the first textbox, and then when in some of the last two.

<html>
    <body>
        <form method="GET" action="" style="display: block">
            <input type="text" id="ctl1" name="ctl1" value="">
            <input type="submit" id="ctl2" name="ctl2" value="Klik">
        </form>
        <form method="GET" action="" style="display: block">
            <input type="text" id="ctl1" name="ctl1" value="">
            <input type="text" id="ctl3" name="ctl3" value="">
            <input type="submit" id="ctl2" name="ctl2" value="Klik">
        </form>
    </body>
</html>

4条回答
唯我独甜
2楼-- · 2019-02-24 23:55

The default behavior for a html form is to submit the form if any control has focus. What you need to do is trap for the ENTER key and then handle form submissions how you want to.

I have done something like this before (there are many sources for this script):

<script language="javascript"> 
function returnkey(evt)  {
    var evt = (evt) ? evt : ((event) ? event : null);
    var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
    if ((evt.keyCode == 13) && (node.type!="textarea"))  {return false;} }

document.onkeypress = returnkey; 
</script>
查看更多
Deceive 欺骗
3楼-- · 2019-02-24 23:56

If you are using asp.net, one option I've used to correct this problem is to set the button to runat="server", wrap your controls in a panel, and set the panel's defaultbutton property to the ID of the button.

查看更多
倾城 Initia
4楼-- · 2019-02-25 00:05

If I were you, I would write the form so that you don't need to worry about the click handler on the button (after all, it wasn't clicked), and also ignore the button's value when the form is submitted (never understood why buttons have values that are submitted).

As a comment to your question points out, the ENTER button's behaviour is not well standardized and you can get different behaviour in different browsers, so even if you think your page works properly it might not for all your users. Also, there are other ways forms can be submitted:

  • Enter key
  • Submit button
  • Javascript form.submit()
  • <input type="image">

For robustness, I'd use the form's onsubmit event handler to do anything that needs to happen when the form is submitted, and only use the onclick handlers to do things that need to happen when a specific button is clicked (most will just submit the form). As for the button value, I'd use an <input type="hidden"> to store whatever hidden value I want for the multiple button scenario, and not bother with the button's value.

However, if those options are not available to you, you can add a hidden input field:

<input type="text" id="h1" name="h1" value="ignore" style="display: none">

This seemed to solve the problem for me on IE8 with your sample code.

查看更多
爷的心禁止访问
5楼-- · 2019-02-25 00:07

If you're using ASP.NET then you should only have a single form. You should also have the runat="server" attribute on any controls you want to access server-side (including the form iteself).

OK, no ASP.NET. It's not posting anything because you've got GET as the method in your form??

查看更多
登录 后发表回答