0

我写了一个带有一些计算的小表格。现在计算是使用按钮单击事件完成的。

  $('a#check_var').click(function() {
    // the below call the form validation  
    $("#bet_cal_form").valid();
          <- My code here ->
  });

现在一切正常,问题是我不希望我的代码执行,除非表单验证没有错误。目前我收到错误消息,但我的代码也被执行。

4

2 回答 2

1

当然,使用if声明。

if ($("#bet_cal_form").valid()) {
    // Stuff that should only be done if form is valid
}
于 2013-11-03T08:20:26.833 回答
0

您需要 if 并取消链接,并且由于所有 ID 都必须是唯一的,因此您不需要选择器中的 a

$(function() {
  $("#check_var").on("click",function(e) {
    e.preventDefault();
    // the below call the form validation  
    if($("#bet_cal_form").valid()) {
      <- My code here ->
    }
  });
});
于 2013-11-03T08:23:13.540 回答