当按下 Enter 时 onsubmit 不会调用我的函数
<form onsubmit="submit(); return false;" name="text" id="form" method="get">
<input type="feild" style="font-size: 150%;" id="field" name="field" value=""
onSubmit="this.value='';"/>
<input type="button" id="add" style="font-size: 150%;" value=" Add tally to score "/>
<input type="submit" value="submit" style="visibility:hidden" />
如果我改变“submit();”到“警报('此处');”它完美地调用了警报功能。 为什么它不调用提交函数?
我希望当用户按 Enter 时调用该函数。 http://jsfiddle.net/kaninepete/JsAr4/
注意:侧面的按钮用于不同的目的,而不是问题的一部分。 编辑 更多的工作,尝试遵循基督徒的榜样让我来到这里 http://jsfiddle.net/kaninepete/MGxNG/
我不明白为什么这与他的例子不同! 我得到的只是 "{"error": "请使用 POST 请求"}" 和他的作品都很完美。
<form onsubmit="submit(); return false;" name="text" id="form" method="get">
<input type="feild" style="font-size: 150%;" id="field" name="field" value=""
onSubmit="this.value='';"/>
<input type="button" id="add" style="font-size: 150%;" value=" Add tally to score "/>
<input type="submit" value="submit" style="visibility:hidden" />
If I change "submit();" to "alert('here');" it calls the alert function perfectly.
Why does it not call the submit function?
I want the function to be called when the user presses enter.
http://jsfiddle.net/kaninepete/JsAr4/
note: The button on the side is for a different purpose, and not part of the problem.
edit
More work, trying to follow Christians example has gotten me here http://jsfiddle.net/kaninepete/MGxNG/
I don't see why this is different than his example!
All I get is "{"error": "Please use POST request"}"
and his works perfectly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
输入元素上没有 onsubmit 方法。
在小提琴上,如果您将输入按钮上的 type="button" 更改为 type="submit" ,那么当您按下按钮时就会调用该表单。
编辑:
你的问题似乎有两件事。
一是
submit
是浏览器中的本机方法。如果您将名称更改为例如。mySubmit
解决了这个问题。第二个是 JSFiddle 将 js 代码放在一个范围内。这意味着您定义的方法在该范围之外将不可用。通过使方法全局化可以解决这个问题:
我的小提琴: http://jsfiddle.net/KrooniX/UttWF/
There is no method onsubmit on input elements.
On fiddle, if you change the type="button" to type="submit" on the input button, then the form is called when you press the button.
Edit:
Your problem seems to be two things.
One is that the
submit
is a native method in the browser. If you change the name to eg.mySubmit
solves the problem.The second is that JSFiddle put the js code inside a scope. This means that the methods you define will not be available outside that scope. This is resoved by making the method global:
My fiddle: http://jsfiddle.net/KrooniX/UttWF/
尝试在 jsfiddle 之外重现,感觉它正在按原样工作。也许我没有得到完整的代码?
这就是我使用的(甚至删除了前面讨论的按钮)。当在文本框中按 Enter 时,此功能有效(在 FF 中测试)。我有什么遗漏的吗?
Tried to reproduce outside of jsfiddle, and it feels like it's working as is. Maybe I didn't get the complete code?
This is what I used (even removed the button discussed earlier). This works when pressing Enter in the textbox (tested in FF). Is there something I'm missing?