带有单个文本字段的 HTML 表单 + 防止 Internet Explorer 中的回发
我注意到 IE 中有一个相当奇怪的行为。
我有一个 HTML 表单,其中有一个输入文本字段和一个提交按钮,
在单击“提交”时,我需要执行执行必要操作的客户端 JavaScript 函数。
现在,当我想阻止文本字段中的回发(按回车键时)。
我添加了一个按键 JavaScript 函数,如下所示:
<input type=text onkeypress="return OnEnterKeyPress(event)" />
function OnEnterKeyPress(event) {
var keyNum = 0;
if (window.event) // IE
{
keyNum = event.keyCode;
}
else if (event.which) // Netscape/Firefox/Opera
{
keyNum = event.which;
}
else return true;
if (keyNum == 13) // Enter Key pressed, then start search, else do nothing.
{
OnButtonClick();
return false;
}
else
return true;
}
奇怪的是,这不起作用。
但是如果我将文本字段传递给函数:
<input type=text onkeypress="return OnEnterKeyPress(this,event);" />
function OnEnterKeyPress(thisForm,event) {
var keyNum = 0;
if (window.event) // IE
{
keyNum = event.keyCode;
}
else if (event.which) // Netscape/Firefox/Opera
{
keyNum = event.which;
}
else return true;
if (keyNum == 13) // Enter Key pressed, then start search, else do nothing.
{
OnButtonClick();
return false;
}
else
return true;
}
我能够阻止回发。
任何人都可以确认这里到底发生了什么吗?
HTML 表单只有一个文本框和一个提交按钮。
提交时执行的 JavaScript 函数的结果输出显示在单独 div 的 HTML 文本区域中。
I have noticed a rather strange behaviour in IE.
I have a HTML form with a single input text field and a submit button
On Submit click I need to execute a client side JavaScript function that does the necessary.
Now when I want to prevent the postback in the text field (on enter key press).
I have added a key press JavaScript function that looks like this:
<input type=text onkeypress="return OnEnterKeyPress(event)" />
function OnEnterKeyPress(event) {
var keyNum = 0;
if (window.event) // IE
{
keyNum = event.keyCode;
}
else if (event.which) // Netscape/Firefox/Opera
{
keyNum = event.which;
}
else return true;
if (keyNum == 13) // Enter Key pressed, then start search, else do nothing.
{
OnButtonClick();
return false;
}
else
return true;
}
Strangly this doesn't work.
But if I pass the text field to the function :
<input type=text onkeypress="return OnEnterKeyPress(this,event);" />
function OnEnterKeyPress(thisForm,event) {
var keyNum = 0;
if (window.event) // IE
{
keyNum = event.keyCode;
}
else if (event.which) // Netscape/Firefox/Opera
{
keyNum = event.which;
}
else return true;
if (keyNum == 13) // Enter Key pressed, then start search, else do nothing.
{
OnButtonClick();
return false;
}
else
return true;
}
I am able to prevent the postback.
Can anyone confirm what is exactly happening here?
The HTML form has just one text box and a submit button.
The resultant output of the JavaScript function executed on submit is displayed in a HTML text area in a separate div.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
必须与您尚未发布的其他代码进行一些交互。 您的代码在两种变体中都停止了我的表单提交,并且没有理由不这样做。
但捕获 Enter 按键很难正确完成,并且可能会像其他任何事情一样令人烦恼。 几乎总有更好的方法。
例如,正如 altCognito 建议的那样,让“form.onsubmit”返回 false,以便表单永远不会提交以响应用户交互。 另外,如果您将 AJAX 代码(假设这就是您正在做的事情)放在 onsubmit 处理程序中而不是 onclick 按钮上,您也可以这样做,以便按 Enter 与单击提交按钮具有相同的效果,用户通常可能期望。
如果您不希望这样,似乎没有理由包含
There must be some interaction with other code you haven't posted. Your code stops the form submission for me in both variants and there is no reason it shouldn't.
But trapping Enter keypresses is difficult to do properly and is likely to annoy as much as anything else. There is almost always a better way.
For example, as altCognito suggests, have ‘form.onsubmit’ return false so the form never submits in response to user interaction. Also, if you put your AJAX code (assuming that's what you're doing) in the onsubmit handler instead of onclick on a button, you can also make it so that pressing Enter has the same effect as clicking on the submit button, which the user might normally expect.
If you don't want that, there seems to be no reason to include the <form> element at all. It's perfectly valid to just have input elements sitting on their own, if you never expect the browser to submit them anywhere.
刚刚来到这里是因为至少对 IE 7 和 8 进行了相关观察:
如果表单上只有一个文本输入和一个提交按钮,并且用户按下返回键,则 IE 将不会包含提交按钮的名称/值对在请求中。 相反,只有文本输入的值会在那里。
如果我添加另一个文本输入,则会发布文本输入和提交按钮的参数。 第二个文本输入甚至可以隐藏:
所以这与事件无关,而是与事件相关到简单的表单提交。
它发生在 GET 和 POST 中。 将选择元素添加到表单不会改变行为。 - 很奇怪。
Have just come here because of a related observation with at least IE 7 and 8:
If there is only one text input and one submit button on a form and the user presses return key, IE will not include the submit button's name/value-pair in the request. Insead, only the text input's value will be there.
If I add another text input, both text inputs and the submit button's parameters are posted. The second text input can even be hidden:
<input type="text" style="display:none"/>
So this is not related to events but rather to simple form submission.
It happens with GET and POST. Adding a select element to the form does not change the behavior. -- very strange.
找到了解决此问题的方法。
我刚刚发现在 IE 中,如果在表单中只有一个文本字段和一个提交按钮,按 Enter 键会导致表单提交。 但是,如果有多个文本框,IE 不会在按下 Enter 时自动回发表单,而是会触发按钮的 onclick 事件。
因此,我在表单中引入了一个隐藏的文本字段,一切都神奇地起作用。
我什至不需要处理文本字段的 onkeypress 事件。
这对我来说非常有效!
这在 FF 中不是问题,因为按 Enter 键会直接调用提交按钮的 onclick 事件。
Found out a work around for this issue.
i just found out that in IE, if,in a form, there is just one text field and one submit button, pressing enter results in form submit. However if there are more than one text boxes, IE doesn't auto postback the form on enter press, instead it fires the button's onclick event.
So i introduce a hidden text field in the form and everything works magically.
I donot even need to handle the onkeypress event for the text field.
This works perfectly for me!!
This is not an issue in FF, as pressing enter directly results in call to submit button's onclick event.
您好,您还可以禁用默认行为。
干杯!
Hi you can also disable the default behavior.
Cheers!
在用于通过 javascript 提交表单的 onsubmit 中返回 false。
Return false in the onsubmit which is used to submit the form via javascript.