javascript - 如何使用 jQuery .submit 提交表单?

标签 javascript jquery forms

我有以下代码:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</head>

<body>
    <form method="post" id="theform" action="http://example.com" target="_blank">
        <input name="email" placeholder="email" type="text">
        <input id="submit" type="submit" value="submit">
    </form>
    <script type="text/javascript">
    jQuery(function() {
        jQuery('#submit').on('click', function(e) {
                e.preventDefault();
                jQuery('#theform').attr('disabled', 'disabled');
                //Code here
                jQuery('#theform').removeAttr('disabled').submit();
            }
        });

    });
    </script>
</body>

</html>

表单未提交。知道发生了什么吗?

我知道我可以执行 ajax 调用来手动将表单提交到操作 URL,然后使用 JavaScript 重定向到我想要将用户发送到新位置的位置标签;但是,我不想这样做,因为弹出窗口拦截器会耗尽JavaScript重定向。因此,我在 submit 上有 target="_blank" 表单,它可以让用户到达我想要将其发送到的位置...只要代码有效。

最佳答案

从 onclick 事件处理程序中删除行 e.preventDefault();

更新: 抱歉,我没有注意到您稍后在代码中明确尝试提交表单。尽管上述更改可以解决该问题,但实际问题在其他地方。不要对函数进行任何更改,只需将提交按钮的 id 重命名为其他名称并更新绑定(bind),代码就应该可以工作。 工作 fiddle :http://jsfiddle.net/epednoat/

<body>
    <form method="post" id="theform" action="http://example.com" target="_blank">
        <input name="email" placeholder="email" type="text">
        <input id="smt" type="submit" value="submit">
    </form>
    <script type="text/javascript">
    jQuery(function() {
        jQuery('#smt').on('click', function(e) {
                e.preventDefault();
                jQuery('#theform').attr('disabled', 'disabled');
                //Code here
                jQuery('#theform').removeAttr('disabled').submit();
            }
        });

    });
    </script>
</body>

</html>

关于javascript - 如何使用 jQuery .submit 提交表单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30522238/

相关文章:

javascript - 使用 php 和 html 关注/取消关注按钮

jquery - 在 jQuery 中预加载背景图像

javascript - Pacta.js 中 $.when 的代数实现

javascript - 使用 Jquery Validator 出现验证错误时如何避免或禁用表单提交

java - 在 Google App Engine 上运行 Jaunt(网络抓取器): Java

javascript - Firefox 在每次请求 Promise ajax 调用时弹出 "This web page is being redirected to a new location"- 有什么办法可以避免吗?

javascript - d3.js map 上的跳动标记点

javascript - Jest "Cannot log after tests are done. Did you forget to wait for something async in your test?"

javascript - 使用 jquery 操作::选择

forms - 是否可以在不将控件放在 VB6 中的窗体上的情况下使用控件?