即使发出警报,框检查?

时间:2016-09-05 15:33:58

标签: javascript jquery

我想设置它。因此,当它超过某个值时,会发出警报以通知用户勾选了太多的框。警报显示成功但有问题的复选框保持勾选状态。我之前使用过这个功能,但这次没用。我不知道为什么。提前感谢您的任何帮助。

function cannotDoThreeATTandAwareness() {
  var i = 0;
  $('.ATT, #aware').each(function() {
    if ($(this).is(':not(:visible)')) {
      i += 3
    }
  });
  $('.ATT').each(function() {
    if ($(this).is(':checked')) {
      i += 3
    }
  });
  $('.awareness').each(function() {
    if ($(this).is(':checked')) {
      i += 1
    }
  });

  if (i > 9) {
    alert('You cannot select this paper as you have already selected your Core 2 paper');
  };

  return i == 10;
}
onclick =" if(cannotDoThreeATTandAwareness()){this.checked=false};"

1 个答案:

答案 0 :(得分:0)

只需return您在Click事件中想要的结果。



function cannotDoThreeATTandAwareness() {
  var i = 0;
  // used 'true' to always make it fail, for the sake of example
  if (true || i > 9) { 
    alert('You cannot select this paper as you have already selected your Core 2 paper');
  };

  return i > 9;
}

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label>
  <input type="checkbox" onclick="return cannotDoThreeATTandAwareness()">
  Click me
</label>
&#13;
&#13;
&#13;

另一种选择是为函数添加一个参数,让我们说e,并且你想要阻止更改,你可以使用{{1} }。为了实现这一点,您需要将事件传递给函数,这是一个片段:

e.preventDefault()
function cannotDoThreeATTandAwareness(e) {
  ...
  if (true || i > 9) {
    e.preventDefault()
    ...
  };
}
相关问题