[英]Hitting “enter” from an input (text) field when a form has a “submit” input
I'm trying to figure out something having to do with the behavior descibed by this old question from last year . 我试图找出与去年这个老问题所描述的行为有关的东西。 I've got a form with a single input text field, and a single "submit" button.
我有一个带有单个输入文本字段和一个“提交”按钮的表单。 Unlike the person who asked that question, I want the form to submit when the user hits "enter".
与提出该问题的人不同,我希望在用户单击“输入”时提交表单。 Indeed, that's working for me;
确实,这对我有用。 what's not working however is the quirk that causes a "submit" input in the form to be virtually clicked: Firefox and Chrome include the "submit" input's name in the parameters posted, while IE does not.
但是,导致该表单中的“提交”输入被虚拟单击的怪癖是不起作用的:Firefox和Chrome在发布的参数中包括了“提交”输入的名称,而IE却没有。 In other words, I want this quirky submit behavior to post the text input field, and the submit input too.
换句话说,我希望这个古怪的提交行为能够发布文本输入字段,并且也提交提交输入。
The funny thing is that my form was doing just that up until some mystery change I introduced recently. 有趣的是,直到最近我进行了一些神秘的更改之前,我的表单一直在这样做。 (Firefox and Chrome do still work, but IE once worked and now doesn't.) I can probably figure out what I changed to break it, but as @bobince seemed to understand something about this quirk I figured I'd ask to see if there are known (possibly even documented) ways to control it.
(Firefox和Chrome仍然可以使用,但IE曾经可以使用,现在却不能使用。)我可能可以弄清楚我为改变它所做的更改,但是由于@bobince似乎对这种怪癖有所了解,所以我想看看是否有已知的(可能甚至有记录)控制它的方法。
Here's my test page: http://gutfullofbeer.net/post.html 这是我的测试页: http : //gutfullofbeer.net/post.html
It's about as simple a test as I could make up: 这几乎是我可以完成的测试:
<!DOCTYPE html>
<html>
<body>
<form name='foo' action='cgi/echoparams.cgi' method='post'>
<input type='text' name='name' value=''>
<input type='submit' name='submit_button' value='Submit'>
</form>
</body>
</html>
The cgi script just parrots back the posted content. cgi脚本只模仿返回的内容。
Yeah, sorry, it's not directly fixable. 是的,很抱歉,它不能直接修复。 I don't know what your previous 'working' case was, but I suspect there might have been an extra input present.
我不知道您以前的“可行”案例是什么,但我怀疑可能会有其他输入。 (It's only when there's a single text input that the exceptional behaviour occurs.)
(仅当有单个文本输入时,才会发生异常行为。)
With the historical mines around the question of whether a default-submit button is 'successful', it's better never to rely on it. 围绕着有关默认提交按钮是否“成功”这一问题的历史遗迹,最好不要依赖它。 Add the hidden input to signify a submission and omit the
name
on the submit button. 添加隐藏的输入以表示提交,并在提交按钮上省略
name
。 Put any submission scripting on form onsubmit
rather than the default button. 将所有提交脚本放在
form onsubmit
而不是默认按钮上。
If you have additional submit buttons for different actions (eg. Edit vs Delete), then make the first button perform the 'default' action and set no name, then add name
/ value
s that override the default behaviour on the other buttons. 如果您还有其他用于不同操作的提交按钮(例如,“编辑”与“删除”),则使第一个按钮执行“默认”操作,不设置名称,然后添加
name
/ value
,以覆盖其他按钮上的默认行为。 These are sure to be 'successful' (and will receive a click
event) if clicked. 如果单击这些,则肯定会“成功”(并会收到
click
事件)。
I just stumbled upon this strange behaviour. 我只是偶然发现了这种奇怪的行为。 I had one case in which the value of the name attribute and the
value
from a button were submitted, and a different case, where the name and value attributes of the button Tag where missing in my POST Data. 我有一种情况,其中提交了name属性的
value
和一个按钮中的value
,而另一种情况是,我的POST数据中缺少按钮Tag的名称和值属性。
(I used the <button ...>
-tag, but it seems that this doesn't matter.) (我使用了
<button ...>
-tag,但这似乎无关紧要。)
After fiddling around quite a while, i finally got the solution, so i registered to let you guys know. 经过一段时间的摆弄,我终于找到了解决方案,所以我注册了,让你们知道。
IE7 & IE8 start submitting the name
& value
of a present button-tag, the just in case of a second <input type="text">
-tag. IE7和IE8开始提交当前按钮标签的
name
和value
,以防第二个<input type="text">
-tag出现。 If there is only one <input type="text">
present in the page and you hit enter within this input-field the name
& value
of the button are missing. 如果页面中仅存在一个
<input type="text">
,而您在此输入字段中按回车键,则按钮的name
和value
将丢失。
Radiobuttons, checkboxes, hidden inputs... don't matter, you need a second <input type="text">
to fix this. 单选按钮,复选框,隐藏的输入...没关系,您需要第二个
<input type="text">
来解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.