jQuery 提交无法与 一起使用
我正在尝试创建一个提交表单的链接。为什么当我将提交输入元素命名为 submit
时,链接不再有效?
//breaks form submission
<input type="submit" value="Submit" name="submit" />
//does not break form submission
<input type="submit" value="Submit" name="xsubmit" />
在 Chrome 中我收到以下错误。
Uncaught TypeError: Property 'submit' of object #<HTMLFormElement> is not a function
不工作
<p>Type 'correct' to validate.</p>
<form method="post" enctype="multipart/form-data">
<input type="submit" value="Submit" name="submit" />
<a id="btn_submit">
<span id="txt_submit">Submit</span>
</a>
</form>
<script>
$("#btn_submit").click(function() {
$("form").submit();
});
$("form").submit(function() {
});
</script>
工作
<p>Type 'correct' to validate.</p>
<form method="post" enctype="multipart/form-data">
<input type="submit" value="Submit" name="xsubmit" />
<a id="btn_submit">
<span id="txt_submit">Submit</span>
</a>
</form>
<script>
$("#btn_submit").click(function() {
$("form").submit();
});
$("form").submit(function() {
});
</script>
I am trying to make a link submit a form. How come when I name a submit input element submit
, the link no longer works?
//breaks form submission
<input type="submit" value="Submit" name="submit" />
//does not break form submission
<input type="submit" value="Submit" name="xsubmit" />
In Chrome I receive the following error.
Uncaught TypeError: Property 'submit' of object #<HTMLFormElement> is not a function
NOT Working
<p>Type 'correct' to validate.</p>
<form method="post" enctype="multipart/form-data">
<input type="submit" value="Submit" name="submit" />
<a id="btn_submit">
<span id="txt_submit">Submit</span>
</a>
</form>
<script>
$("#btn_submit").click(function() {
$("form").submit();
});
$("form").submit(function() {
});
</script>
WORKING
<p>Type 'correct' to validate.</p>
<form method="post" enctype="multipart/form-data">
<input type="submit" value="Submit" name="xsubmit" />
<a id="btn_submit">
<span id="txt_submit">Submit</span>
</a>
</form>
<script>
$("#btn_submit").click(function() {
$("form").submit();
});
$("form").submit(function() {
});
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“提交”是保留字。它与javascript冲突。只需将输入的名称更改为其他名称即可。
来自方法 submit() 的 jQuery 文档:
"Submit" is a reserved word. It conflicts with javascript. Just change the name of the input to something else.
From the jQuery documentation on the method submit():
两个更改:
首先,将绑定包含在 document.ready 处理程序中
$(function () {...});
其次,在事件上添加 PreventDefault 调用以停止正常的锚点功能。
Two changes:
First, include the bind in the document.ready handler
$(function () {...});
Second, add the preventDefault call on the event to stop the normal anchor functionality.