大家好,这个任务似乎很容易,但我的代码不起作用。
我有一些行的表,每行都有一个复选框,在下面我按下“全部删除”按钮取消选中复选框。 这是我的代码:
$('#remove-button').click(function(){
$('input:checked').prop('checked',false);
//e.stopPropagation(); // I tried also with this
// $('input:checked').removeAttr('checked'); // or this syntax
});
当我点击按钮时,所有输入都未被选中,但由于我的代码不会发生这种情况,因为当我评论此片段时,当我点击按钮时也会发生这种情况。我也看到,当我点击并且输入未被选中时,我的网站似乎刷新了,因为页面会被扫描。
我还为检查输入添加了另一个按钮:
$('#add-button').click(function(e){
$('input').prop('checked',true);
//e.stopPropagation(); // I tried also with this
// $('input:checked').removeAttr('checked'); // or this syntax
});
当我点火时,这些输入被检查,一秒钟我的网站刷新,一切都消失了。
我使用最新的Firefox和Chrome,以及jQuery - 1.11(但也检查了1.8.0和1.7.1)。 还有无法接受的
任何人都知道出了什么问题?
答案 0 :(得分:5)
'检查' property是boolean但它意味着当它不存在时它是false。
如果您不需要点击提交或者不进行链接,则会e.preventDefault()
。
所以你需要这个:
$('#remove-button').click(function(e) {
$('input:checked').removeAttr('checked');
e.preventDefault();
});
$('#add-button').click(function(e) {
var input = $('input.input-to-check')
input.attr('checked', 'checked');
e.preventDefault();
});