簡體   English   中英

jQuery的checkall復選框加重置

[英]jquery checkall checkbox plus reset

我正在使用以下jQuery來實現檢查PHP頁面中的所有效果:

<script>
$(function(){
    $("#checkall").click(function(){
        if($("#checkall").attr('checked')){
            $(".department").attr('checked', true);
        }else{
            $(".department").attr('checked', false);
        }
    });
});
</script>

<input type="checkbox" id="checkall" name="check_all" />

<input type="checkbox" name="department[]" class="department" value=""  checked = "checked"/>

並且選中全部和取消選中所有效果都很好。 但是,它不能與表單中的“重置”按鈕一起使用。 我應該如何修改jQuery? 謝謝!

試試這個演示: http : //jsfiddle.net/JCWaT/

我在這里的舊回復: 選中所有復選框

希望符合需要:)

或您的需要 http://jsfiddle.net/kJCcP/

$('input[name="Company"],input[name="Country"]').click(function(){

          $(this).nextAll('input[name="'+this.name+'Sub"]').prop('checked',this.checked);

});
​

要么

$('#checkall').click(function() {

    $(this).nextAll('.department').prop('checked', this.checked);

});​

使用.prop代替.attr ,請參閱工作演示

$(function(){
    $("#checkall").click(function(){
        if($(this).prop('checked')){
            $(".department").prop('checked', true);
        }else{
            $(".department").prop('checked', false);
        }
    });
});​

而且您的代碼可能甚至很短:

$(function(){
    $("#checkall").click(function(){
        $(".department").prop('checked', $(this).prop('checked'));
    });
});​
$("#checkall").click(function(){
    $(".department").prop('checked', $(this).is(':checked') ); 
});

jQuery .prop()文檔中有一節描述了為什么應使用它而不是操縱屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM