检测表单中的后期操作

时间:2011-11-27 17:23:24

标签: javascript javascript-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>

1 个答案:

答案 0 :(得分:2)

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>