Enter button does not submit form (IE ONLY) ASP.NET

后端 未结 9 805
夕颜
夕颜 2020-12-01 10:17

I have a form with a textbox and a button. IE is the only browser that will not submit the form when Enter is pressed (works in FF, Opera, Safari, Chrome, etc.). I found th

相关标签:
9条回答
  • 2020-12-01 10:19

    Just create a text input in a hidden div on the page. This will circumvent the IE bug.

    Example div:

        <!-- Fix for IE bug (One text input and submit, disables submit on pressing "Enter") -->
        <div style="display:none">
                <input type="text" name="hiddenText"/>
        </div>
    
    0 讨论(0)
  • 2020-12-01 10:25

    The other thing I have done in the past is wrap the form area in a Panel and set the DefaultButton attribute to the submit button you have. This effectively maps the enter key to the submission as long as you have a form element in focus in the panel area.

    0 讨论(0)
  • 2020-12-01 10:26

    Does it use a GET instead of a POST? Is the URL too long? I've seen that...

    0 讨论(0)
  • 2020-12-01 10:30

    When using display:none, IE won't see the button and therefore won't be able to use it to submit the form. Instead, you could use z-index and absolute positioning to hide it under another element, e.g. with the style:

    position:absolute; bottom: -20px; left: -20px; z-index: -1;

    Now it'll still be there, usable by IE, but hidden beneath another element.

    0 讨论(0)
  • 2020-12-01 10:34

    Basically, a form needs either a button, input type="submit" or an input type="image" to enable the builtin behaviour to submit a form on enter. You shouldn't need a javascript to submit it.

    0 讨论(0)
  • 2020-12-01 10:35
    // Use the following Javascript in your HTML view
    // put it somewhere between <head> and </head>
    
        <script language="JavaScript" type="text/javascript"><!--
        function KeyDownHandler(btn)
        {
          if (event.keyCode == 13)
          {
            event.returnValue=false;
            event.cancel = true;
            btn.click();
          }
        }
        // -->
        </script>
    
        // Put this in your TextBox(es) aka inside <asp:textbox ... >
        onkeydown="KeyDownHandler(ButtonID)"
    
    0 讨论(0)
提交回复
热议问题