jQuery 提交无法与 一起使用

发布于 2024-12-21 23:58:35 字数 1364 浏览 2 评论 0原文

我正在尝试创建一个提交表单的链接。为什么当我将提交输入元素命名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半暖夏伤 2024-12-28 23:58:35

“提交”是保留字。它与javascript冲突。只需将输入的名称更改为其他名称即可。

来自方法 submit() 的 jQuery 文档:

表单及其子元素不应使用以下输入名称或 ID
与表单的属性冲突,例如提交、长度或方法。
名称冲突可能会导致混乱的故障。如需完整列表
规则并检查您的标记是否存在这些问题,请参阅 DOMLint。

"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():

Forms and their child elements should not use input names or ids that
conflict with properties of a form, such as submit, length, or method.
Name conflicts can cause confusing failures. For a complete list of
rules and to check your markup for these problems, see DOMLint.

霓裳挽歌倾城醉 2024-12-28 23:58:35

两个更改:

$(function () {

$("#btn_submit").click(function(e) {
     e.preventDefault();
     $("form").submit();
    });

    $("form").submit(function() {

    });

});

首先,将绑定包含在 document.ready 处理程序中 $(function () {...});
其次,在事件上添加 PreventDefault 调用以停止正常的锚点功能。

Two changes:

$(function () {

$("#btn_submit").click(function(e) {
     e.preventDefault();
     $("form").submit();
    });

    $("form").submit(function() {

    });

});

First, include the bind in the document.ready handler $(function () {...});
Second, add the preventDefault call on the event to stop the normal anchor functionality.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文