jQuery在类顺序中按编号禁用复选框

时间:2013-11-06 07:04:24

标签: jquery class checkbox

我有一系列复选框:

<input class="Spec" type="checkbox" value="1" />
<input class="Spec" type="checkbox" value="2" />
<input class="Spec" type="checkbox" value="5" /> 
<input class="Spec" type="checkbox" value="8" /> 
<input class="Spec" type="checkbox" value="10" />  
<input class="Spec" type="checkbox" value="30" />

有几百个(不要问)。

使用jQuery我可以使用class启用和禁用复选框,但我需要启用/禁用class="Spec"显示的第二个和第五个复选框。

是否有可能,是孩子/父母等吗?

8 个答案:

答案 0 :(得分:2)

尝试

 $('.Spec').filter(':eq(1) , :eq(4)').prop('disabled', true);

eq(1)eq(4)会同时停用第二个第五个复选框。或者您可以单独执行此操作

$('.Spec:eq(1)').prop('disabled', true);
$('.Spec:eq(4)').prop('disabled', true);  

也应该是这样的

$('.Spec').eq(1).prop('disabled', true);
$('.Spec').eq(4).prop('disabled', true);

答案 1 :(得分:1)

您可以使用:eq()选择器(基于索引)执行此操作:

$('.Spec:eq(1)').prop('disabled', true);  // disable the second checkbox
$('.Spec:eq(4)').prop('disabled', true);  // disable the fifth checkbox

答案 2 :(得分:0)

我认为最好的解决方案是当你设置html然后添加一个类来禁用复选框 像

<input class="Spec" type="checkbox" value="1" />
<input class="Spec disclass" type="checkbox" value="2" />
<input class="Spec" type="checkbox" value="5" /> 
<input class="Spec" type="checkbox" value="8" /> 
<input class="Spec disclass" type="checkbox" value="10" />  
<input class="Spec" type="checkbox" value="30" />

然后在Js

$(".disclass").attr("disabled","disabled");

或者你可以尝试,如果你不想添加类

$('.Spec')
.filter(':eq(1), :eq(4)')
.attr('disabled','disabled');// by attr


 // or can try
 $('.Spec')
    .filter(':eq(1), :eq(4)')
    .prop('disabled',true);

参考 attr prop filter

答案 3 :(得分:0)

使用CSS3 nth-child选择器。例如。第二:

$('.Spec>:nth-child(2)').attr("disabled","disabled");

类似于第五个复选框

答案 4 :(得分:0)

//use true to enable and false to disable
    var arrChk = $('.Spec');
    arrChk[1].checked = true;
    arrChk[4].checked = true;

答案 5 :(得分:0)

试试这个;

$("#button").toggle(function(){
    $( "input:eq(4)" ).attr("checked",true).addClass("selected");
}, function(){
    $( "input:eq(4)" ).attr("checked",false).removeClass("selected");
});    

attr()或prop(),如果你有jquery 1.9.1 =&gt;你必须使用prop()。

http://jsfiddle.net/mehmetakifalp/WyRfF/

答案 6 :(得分:0)

You can use following code to disable specific checkbox

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
    $( document ).ready(function() {
        var len = $('.Spec').length;
        var count = 1;
        $( ".Spec" ).each(function() {
            if (count == 2 || count == 5) {
                $(this).attr('disabled', true);
            }
            count++;
        });
    });
</script>

You can use following code to enable specific checkbox

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script>
    $( document ).ready(function() {
        var len = $('.Spec').length;
        var count = 1;
        $( ".Spec" ).each(function() {
            if (count == 2 || count == 5) {
                $(this).removeAttr("disabled");
            }
            count++;
        });
    });
</script>

答案 7 :(得分:0)

这样的事情应该做你想做的事情:

$("input.Spec[value=2]").attr('checked', true);
$("input.Spec[value=10]").attr('checked', true);

请注意,这将选中值为2和10的所有复选框(如果您有多个具有相同值的复选框)

如果你不知道这些值或者从上面不想要这个,那么也许另一种方法是为你的复选框自动生成ID,如下所示:

<input id="c1" class="Spec" type="checkbox" value="1" />
<input id="c2" class="Spec" type="checkbox" value="2" />
<input id="c3" class="Spec" type="checkbox" value="5" /> 
<input id="c4" class="Spec" type="checkbox" value="8" /> 
<input id="c5" class="Spec" type="checkbox" value="10" />  
...

然后有这样的事情:

$("#c2").attr('checked', true);
$("#c5").attr('checked', true);

希望这会对你有所帮助。