javascript - 检测表单中的后期操作

标签 javascript jquery jquery-events

我想在提交表单时执行一些操作。

var ispostaction = false;
$("#myform").submit(function () {
 ispostaction = true;
});

提交表单时,不会调用 .submit(function ())。 我这样做有什么问题吗?我的表单 ID 为 myform。

如果有任何帮助,我将不胜感激。

这是我的 xhtml 页面。我正在使用 JSF 2

<form id="myform" class="someclass" enctype="application/x-www-form-urlencoded"
action="pagename.jsf" method="post">
// custom input text field
// selectOneMenu
// a few more input text fields 
// submit button is below
<input id="javax.faces.ViewState" type="hidden" autocomplete="off" value="...." name="javax.faces.ViewState">
</form>

最佳答案

jquery 文档:

The submit event is sent to an element when the user is attempting to submit
a form. It can only be attached to <form> elements. Forms can be submitted
either by clicking an explicit <input type="submit">, <input type="image">,
or <button type="submit">, or by pressing Enter when certain form elements
have focus.

调用submit函数不会触发submit事件。您可以通过添加一个隐藏按钮来“修复”此问题,您可以从 jquery 单击该按钮。不幸的是,大多数(如果不是全部)浏览器都会显示相同的行为。

<html>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<body>
<form id="myform" enctype="application/x-www-form-urlencoded"
action="posturl" method="post">
// custom input text field
// selectOneMenu
// a few more input text fields 
// submit button is below
<input id="javax.faces.ViewState" type="hidden" autocomplete="off" value="...." name="javax.faces.ViewState">
<input type="text" value="a value" />
<input id="submit" type="submit" value="submit" style="display: none;" />
</form>

<script type="text/javascript">
$(document).ready(function() {
    $("#myform").bind('submit', function() {
        alert('');
    });
    $("#submit").click();
});
</script>

</body>
</html>

关于javascript - 检测表单中的后期操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8287429/

相关文章:

php - Bootstrap 移动菜单不显示在 wordpress 中

javascript - 如何使用 iframe 在鼠标悬停时触发事件

javascript - 重复下拉 Javascript 对第二个下拉菜单元素的影响

javascript - Angular : how do I call a scope method on state change?

javascript - ng-animate 没有按预期工作

javascript - MVC4 客户端验证和 ajax

jquery - 尝试在 DIV 单击上展开/折叠 jQuery 菜单

javascript - jQuery.load();并不总是用图像射击

javascript - css api 的 jquery if else 条件

javascript - 在 javascript 函数中发送此参数