检测表单中的post动作

Detect post action in a form

本文关键字:post 动作 表单 检测      更新时间:2023-09-26

我想在表单提交时做点什么。

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>