2

根据我在这里找到的内容,我已经尝试了几个小时的不同方法,但无济于事。有人可以引导我朝着正确的方向前进吗?在检查或取消选中复选框时,我想丢弃确认书,然后根据用户选择,然后将复选框返回或取消选中。谢谢!

$('input:checkbox').click(function(){
     var checked = $("input:checked").length; 
    if (checked == 0){ 
if(confirm('Are you sure you want to discontinue this program?')){
    (':checkbox:checked').removeAttr('checked');
}
    }  else

    { 
if(confirm('Are you sure you want to resume use of this program?'))            
(':checkbox:checked').attr('checked');
    } 

HTML

<table>
  <tr>
    <td><input name="input" type="checkbox" value="" /></td>
    <td>Because We Care</td>
   </tr>
      <tr>
                     <td><input name="" type="checkbox" value="" /></td>
                     <td>Some Unused Program</td>
                     </tr>

4

3 回答 3

8

您的代码中有一些语法和逻辑缺陷,试试这个:

$('input:checkbox').click(function(){
    var checked = $(this).is(':checked');
    if(checked) {
        if(!confirm('Are you sure you want to resume use of this program?')){         
            $(this).removeAttr('checked');
        }
    } else if(!confirm('Are you sure you want to discontinue this program?')){
        $(this).attr("checked", "checked");
    }
}​);​

JSFIDDLE

于 2012-08-06T16:43:52.077 回答
1

执行侦听器时,复选框已选中或未选中。你应该做的是:

  • 当用户确认什么都不做时
  • 当用户不确认撤消已完成的操作时
于 2012-08-06T16:39:16.090 回答
0

这应该正是您正在寻找的:

<!doctype html>
<html>
<input id="chkTest" type="checkbox" />
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.7.2.min.js"></script>
<script type="text/javascript">
    $("#chkTest").change(function(evt){
        var attr = $(evt.target).attr("checked")
        if (typeof attr !== 'undefined' && attr !== false){
            alert("Checked");
        }
        else{
            alert("Not Checked");
        }
    });
</script>
</html>
于 2012-08-06T16:46:50.283 回答