IE需要2个文本框来提交按钮? [英] IE needs 2 textboxes to submit a button?

查看:80
本文介绍了IE需要2个文本框来提交按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了这个奇怪的问题,我不确定是应该责怪ASP.NET还是IE 7 ...

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

想法是这样的:当我的表单上只有一个文本框和一个(提交)按钮时,然后在文本框内部按Enter即可发布文本框的值.该按钮不会提交,也不会触发服务器端点击.

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.

当我有两个文本框时,在其中任何一个框上按ENTER键也将发送按钮并触发Click事件.

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

WTF?

已添加:似乎是与IE相关的问题.这是一个简单的示例进行演示.请注意地址栏,并在第一个文本框和后两个文本框中的某些位置时按ENTER键.

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>

推荐答案

如果我是你,我会编写表格,这样您就不必担心按钮上的单击处理程序了(毕竟,它不是'单击),并且在提交表单时也忽略按钮的值(从未理解为什么按钮具有提交的值).

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:

  • 输入键
  • 提交按钮
  • JavaScript form.submit()
  • <input type="image">
  • Enter key
  • Submit button
  • Javascript form.submit()
  • <input type="image">

出于鲁棒性,我将使用表单的onsubmit事件处理程序来执行提交表单时需要执行的任何操作,并且仅使用onclick处理程序来执行单击特定按钮时需要执行的操作(大多数只需提交表格).至于按钮值,我将使用<input type="hidden">来存储多按钮方案中我想要的任何隐藏值,而不必理会按钮的值.

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

这似乎可以用您的示例代码为IE8解决我的问题.

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

这篇关于IE需要2个文本框来提交按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆