当前位置:编程学习 > JAVA >>

捕获js表单提交事件

当我们点击type=submit的按钮时,表单会提交,并且会触发submit事件(绑定的或者onsubmit)。但是如果用js来提交表单就不会触发submit事件,请问如何捕获js提交表单事件?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>
<script src="jquery-1.6.4.min.js" type="text/javascript"></script>
<script>
function submitForm () {
document.testForm.submit();
}

function disableForm (form) {
alert("!");
if(typeof jQuery.data(form, "disabledOnSubmit") == 'undefined') {
  jQuery.data(form, "disabledOnSubmit", { submited: true });
  $('input[type=submit], input[type=button]', this).each(function() {
$(this).attr("disabled", "disabled");
  });
  return true;
}
else
{
  return false;
}
}

$().ready(function(){
$("form").submit(function() {
alert("!");
if(typeof jQuery.data(this, "disabledOnSubmit") == 'undefined') {
  jQuery.data(this, "disabledOnSubmit", { submited: true });
  $('input[type=submit], input[type=button]', this).each(function() {
$(this).attr("disabled", "disabled");
  });
  return true;
}
else
{
  return false;
}
   });

$("#test").click(function(){

})
})
</script>
</head>
<body>
<form id="testForm" name="testForm" onsubmit="disableForm(this)">
     <input type="button" value="submit by js" title="you can press the button to submit by javascript" onclick="submitForm ()"/>
        <input type="submit" value="submit" title="you can press the button to submit" />
        <input type="button" value="ok" id="test"/>
    </form>
</body>
</html>
JavaScript --------------------编程问答-------------------- 不是通过你点击按键中的那个onclike里面的方法捕捉吗,在js中根据id捕捉啊 --------------------编程问答-------------------- onsubmit 事件会在表单中的确认按钮被点击时发生。
你的js中 $("#test").click(function(){
             //?????????
        }) --------------------编程问答-------------------- 如果你用js提交表单,我建议可以尝试使用ajax的方式提交啊,利用jquery将其序列化[$("#id").serialize();]作为数据传递就可以了啊。
如果你非要使用js控制的要使用 document.getElementById("actorCard").sumbit(); 这样的方式触发提交事件。有个参考的案例你可以看下。
http://solodu.iteye.com/blog/421587 --------------------编程问答--------------------  <a href="javascript:submitForm()">

   function submitForm () {
        document.testForm.submit();
    }
补充:Java ,  Web 开发
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,