根据我在这里找到的东西,我已经尝试了几个小时的不同方法,但无济于事。有人可以引导我朝着正确的方向前进吗?在选中或取消选中复选框时,我想抛出确认,然后根据用户选择选中复选框或选中复选框。谢谢!
$('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>
答案 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");
}
});
答案 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>