根据我在这里找到的内容,我已经尝试了几个小时的不同方法,但无济于事。有人可以引导我走向正确的方向吗?当选中或取消选中复选框时,我想抛出确认,然后让复选框根据用户的选择返回选中或取消选中。谢谢!
$('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");
}
});
关于jquery - 选中复选框后确认框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11832322/