javascript更改表单onsubmit动态 [英] javascript change form onsubmit dynamically

查看:272
本文介绍了javascript更改表单onsubmit动态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有一些操作和onsubmit值的表单,它是通过提交输入标签提交的。问题是它应该通过两个按钮提交,所以我为第二个按钮写了一个函数来改变动作并提交以下形式的值:

I have a form with some action and onsubmit values, which is submitted through a submit input tag. The problem is that it should be submittable by two buttons, so I wrote a function for the second button to change the action and onsubmit values of the form:

<a href="javascript:submitCompare()" class="submit">Compare</a>

function submitCompare()
{
    document.myForm.action = "anotherAction.php";
    document.myForm.onsubmit = function() {return countChecked()};
    document.myForm.submit();
}

function countChecked()
{
  var n = $(".reports input:checked").length;
  if (n >= 3 ) {
    alert ('You must select less than 3 reports.');
    return false;
  }
  else return true;
}

点击比较链接后,我会正确地发送给anotherAction.php页面,但即使我有超过2个选中的复选框(这是验证规则)。有人可以帮我让onsubmit函数正常工作吗?

After clicking on the Compare link it sends me to the anotherAction.php page correctly, but even when I have more than 2 selected checkboxes (which is the validation rule). Can somebody help me make the onsubmit function work correctly?

推荐答案

$ c>,您明确无条件地调用

In submitCompare(), you explicitly and unconditionally call

 document.myForm.submit();

你可能想要的是

 if (countChecked()) {
   document.myForm.submit();
 }

这篇关于javascript更改表单onsubmit动态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆