选中复选框后确认框

时间:2012-08-06 16:32:50

标签: jquery checkbox confirm

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

$('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>

3 个答案:

答案 0 :(得分:6)

有一些语法&amp;代码中的逻辑缺陷,试试这个:

$('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

答案 1 :(得分:1)

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

  • 当用户确认无所事事时
  • 当用户未确认撤消已执行的操作时

答案 2 :(得分: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>