jquery - 选中复选框后确认框

标签 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>

最佳答案

您的代码中存在一些语法和逻辑缺陷,请尝试以下操作:

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

关于jquery - 选中复选框后确认框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11832322/

相关文章:

jquery - 如何使用 Ajax 提交 Flask-WTF 表单

javascript - 页面刷新后保持单击的复选框状态

javascript - 需要在 ASP.NET 回发逻辑期间发出 jQuery 确认消息

javascript - 防止 ng-click 触发确认对话框(在 Angular 1.2 RC3 中停止工作)

jquery - map 区域标签上的工具提示

jquery - 简单 JQuery 全部展开 全部折叠 切换

javascript - 复选框不显示选中

javascript - 使用 onbeforeunload 函数内的自定义对话框确认重新加载页面。弹框前onbeforeunload函数中执行的代码

javascript - jquery 代码未在 Android 手机上运行

c# - 如何在 c#net 中单击按钮将所选项目从一个 ListView 复制到另一个 ListView ?