这个任务看起来很简单,尽管我的代码不起作用。
我有一些行的表格,每行都有一个复选框,在下面我放置了“删除全部”按钮来取消选中复选框。
这是我的代码:
$('#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)。
还有附上
谁知道出了什么问题吗?
最佳答案
'checked' 属性是 bool 值,但它意味着当它不存在时它是 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();
});
关于jquery 按钮取消选中复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25073700/