输入按钮不提交表单(仅提交)ASP.NET

时间:2022-04-09 08:38:03

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 this javascript function to try to coax IE into behaving; but no avail:

我有一个带有文本框和按钮的表单。IE是唯一在按下Enter时不会提交表单的浏览器(适用于FF、Opera、Safari、Chrome等)。我找到了这个javascript函数,试图诱使IE做出行为;但毫无效果:

function checkEnter(e){
    var characterCode
    if (e && e.which) {
        e = e
        characterCode = e.which
    } else {
        e = event
        characterCode = e.keyCode
    }
    if (characterCode == 13) {
        document.forms[0].submit()
        return false
    } else {
        return true
    }
}

Implementation:

实现:

searchbox.Attributes("OnKeyUp") = "checkEnter(event)"

Any advice?

任何建议吗?

EDIT: This page on CodeProject outlines what Dillie was saying, and it works perfectly.

编辑:在CodeProject上的这一页概述了Dillie所说的内容,并且它运行得非常好。

9 个解决方案

#1


13  

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.

我过去做的另一件事是将表单区域包装在一个面板中,并将DefaultButton属性设置为您拥有的submit按钮。这有效地将enter键映射到提交,只要您在面板区域中有一个表单元素。

#2


25  

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

只需在页面上的隐藏div中创建一个文本输入。这将绕过IE bug。

Example div:

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>

#3


3  

There is a good write up of this problem here, and a nice jquery based solution:

这里有一个很好的关于这个问题的描述,以及一个很好的基于jquery的解决方案:

http://www.thefutureoftheweb.com/blog/submit-a-form-in-ie-with-enter

http://www.thefutureoftheweb.com/blog/submit-a-form-in-ie-with-enter

#4


2  

// 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)"

#5


1  

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:

使用display:none时,IE不会看到按钮,因此无法使用它提交表单。相反,你可以使用z-index和绝对定位来隐藏它在另一个元素下,例如:

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

位置:绝对的;底部:-20 px;左:-20 px;z - index:1;

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

现在它仍然在那里,IE可以使用,但是隐藏在另一个元素下面。

#6


1  

Hide the button - not using display:none, but with the following styles:

隐藏按钮—不使用display:none,但是使用以下样式:

position: absolute; /* no longer takes up layout space */
visibility: hidden; /* no longer clickable / visible */

If you do this, you won't need to add any other elements or hidden inputs.

如果这样做,就不需要添加任何其他元素或隐藏的输入。

#7


0  

This is due to a peculiarity in IE for single text field inputs.

这是由于IE中单个文本字段输入的特性。

A simple solution is to stop the page having a single text field by adding another hidden one.

一个简单的解决方案是通过添加另一个隐藏字段来阻止页面具有单个文本字段。

<input type="text" name="hidden" style="visibility:hidden;display:none;" />

see.. http://www.4guysfromrolla.com/articles/060805-1.aspx

看到. .http://www.4guysfromrolla.com/articles/060805 - 1. aspx

#8


-1  

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

它使用GET而不是POST吗?URL太长了吗?我看过……

#9


-1  

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.

基本上,表单需要一个按钮,输入类型="submit"或输入类型="image",以使构建行为在输入时提交表单。您不需要javascript来提交它。

#1


13  

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.

我过去做的另一件事是将表单区域包装在一个面板中,并将DefaultButton属性设置为您拥有的submit按钮。这有效地将enter键映射到提交,只要您在面板区域中有一个表单元素。

#2


25  

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

只需在页面上的隐藏div中创建一个文本输入。这将绕过IE bug。

Example div:

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>

#3


3  

There is a good write up of this problem here, and a nice jquery based solution:

这里有一个很好的关于这个问题的描述,以及一个很好的基于jquery的解决方案:

http://www.thefutureoftheweb.com/blog/submit-a-form-in-ie-with-enter

http://www.thefutureoftheweb.com/blog/submit-a-form-in-ie-with-enter

#4


2  

// 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)"

#5


1  

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:

使用display:none时,IE不会看到按钮,因此无法使用它提交表单。相反,你可以使用z-index和绝对定位来隐藏它在另一个元素下,例如:

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

位置:绝对的;底部:-20 px;左:-20 px;z - index:1;

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

现在它仍然在那里,IE可以使用,但是隐藏在另一个元素下面。

#6


1  

Hide the button - not using display:none, but with the following styles:

隐藏按钮—不使用display:none,但是使用以下样式:

position: absolute; /* no longer takes up layout space */
visibility: hidden; /* no longer clickable / visible */

If you do this, you won't need to add any other elements or hidden inputs.

如果这样做,就不需要添加任何其他元素或隐藏的输入。

#7


0  

This is due to a peculiarity in IE for single text field inputs.

这是由于IE中单个文本字段输入的特性。

A simple solution is to stop the page having a single text field by adding another hidden one.

一个简单的解决方案是通过添加另一个隐藏字段来阻止页面具有单个文本字段。

<input type="text" name="hidden" style="visibility:hidden;display:none;" />

see.. http://www.4guysfromrolla.com/articles/060805-1.aspx

看到. .http://www.4guysfromrolla.com/articles/060805 - 1. aspx

#8


-1  

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

它使用GET而不是POST吗?URL太长了吗?我看过……

#9


-1  

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.

基本上,表单需要一个按钮,输入类型="submit"或输入类型="image",以使构建行为在输入时提交表单。您不需要javascript来提交它。