jquery按钮取消选中复选框

kub*_*506 5 jquery button unchecked

大家好,这个任务似乎很容易,虽然我的代码不起作用.

我有一些行的表,每行有一个复选框,在下面我把按钮"全部删除"取消选中复选框.这是我的代码:

    $('#remove-button').click(function(){
        $('input:checked').prop('checked',false);
        //e.stopPropagation(); // I tried also with this
        // $('input:checked').removeAttr('checked'); // or this syntax
    });
Run Code Online (Sandbox Code Playgroud)

当我点击按钮时,所有输入都是未选中的,但由于我的代码不会发生这种情况,因为当我评论此片段时,当我点击按钮时也会发生这种情况.我也看到,当我点击并且输入未被选中时,我的网站似乎刷新了,因为页面空洞.

我还添加了另一个按钮来检查输入:

    $('#add-button').click(function(e){
        $('input').prop('checked',true);
        //e.stopPropagation(); // I tried also with this
        // $('input:checked').removeAttr('checked'); // or this syntax
    });
Run Code Online (Sandbox Code Playgroud)

当我点火这个输入被检查,一秒钟我的网站刷新,一切都消失了.

我使用最新的Firefox和Chrome,以及jQuery - 1.11(但也检查了1.8.0和1.7.1).还有一些问题

谁知道什么是错的?

Lyu*_*man 5

'checked'属性是布尔值但它意味着当它不存在时它是假的.如果你不需要点击提交或不去链接那里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();
 });
Run Code Online (Sandbox Code Playgroud)