CheckBox Jquery - 提示不起作用

时间:2016-10-27 11:22:34

标签: javascript jquery checkbox

我之前有过这个工作但由于某种原因它已经停止工作,我认为是语法错误。有谁能发现这个问题?

我已经让它在浏览器中插入源代码并在控制台中插入Jquery。但不是在JSFiddle和其他一些选项中。

勾选时应该提示,如果未勾选则再次提示:

<td class="icon">
            <input type="checkbox" id="my_checkbox">
    </td>


    $("#my_checkbox").click(function() {
      if (this.checked) {
        this.checked = confirm('Are you sure you want to CONFIRM the order and EMAIL THE CUSTOMER?'); 
      }    
      else {
        this.checked = !confirm('Do you really want to change this to NOT RECEIVED?'); 
      }  
    });

https://jsfiddle.net/thdbfuhs/

3 个答案:

答案 0 :(得分:1)

没有jquery

&#13;
&#13;
$("#my_checkbox").click(function() {
  if (document.getElementById('my_checkbox').checked) {
    this.checked = confirm('Are you sure you want to CONFIRM the order and EMAIL THE CUSTOMER?');
  }    
  else {
    this.checked = !confirm('Do you really want to change this to NOT RECEIVED?');
  }  
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td class="icon">
        <input type="checkbox" id="my_checkbox">
</td>
&#13;
&#13;
&#13;

使用jquery

&#13;
&#13;
$("#my_checkbox").click(function(e) {
  if ($(this).is(':checked')) {
    this.checked = confirm('Are you sure you want to CONFIRM the order and EMAIL THE CUSTOMER?');
   
  }    
  else {
    
     e.preventDefault();
  }  
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td class="icon">
        <input type="checkbox" id="my_checkbox">
</td>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

你错过了&#34;)&#34;在JSFiddle的JavaScript末尾分号之前。粘贴在您的问题中的JavaScript是正确的。

答案 2 :(得分:0)

首先在你的html中包含jquery.js。然后尝试下面的“测试”代码。

$("#my_checkbox").on("click",function() {
  if ( $(this).is(':checked')) {
  alert("checked");
  } else {
  alert("Not checked");
}
});