Limiting Default Checked checkboxes

2019-05-28 21:48发布

I have problem regarding on how i will limit my default checked checkbox. for example i have 7 checkboxes. and i want to limit it with 3 default checked checkboxes once the page is load.

this should be the output:

Checkbox1 : true
Checkbox2 : true
Checkbox3 : true
Checkbox4 : false
Checkbox5 : false
Checkbox6 : false
Checkbox7 : false

Here's my sample code:

var mvp = 3;
$(document).ready(function() {
$("input:checkbox").each(function( index ) {
    ($this).attr("checked",true);
});

});

I'm stock with this, i don't know where i will put my counter (mvp) inside my each function. in this code, all my checkboxes are checked :D.

Sorry for a newbie question, please help me..

7条回答
成全新的幸福
2楼-- · 2019-05-28 22:35

Decrement mvp after you check each checkbox. When it reaches 0, start unchecking.

$(document).ready(function() {
    var mvp = 3;
    $("input:checkbox").each(function( index ) {
        ($this).attr("checked", mvp > 0);
        mvp--;
    });
});
查看更多
登录 后发表回答