关于radiobutton计数,然后使用jquery更改类

时间:2013-05-01 03:46:23

标签: javascript jquery asp.net

这是html的标记

<input type="checkbox" title="enable" value="30"/>
<input type="checkbox" title="enable" value="31"/>
<input type="checkbox" title="disable" value="32"/>
<input type="checkbox" title="disable" value="33"/>
<input type="checkbox" title="disable" value="34"/>
<a class="testdisable" id="testhere">TESTCLASS</a>

现在我想要禁用所选复选框中的任何一个禁用类。如果选中了两个启用复选框,那么我希望在锚点上应用禁用类。如果选中了两个禁用复选框,那么我想要启用类应用于锚标签。 我尝试了以下jquery代码..但没有得到正确的结果..我在两个禁用和两个禁用和一个启用时遇到问题

MY JS FIDDLE: - http://jsfiddle.net/TpBGG/1/

2 个答案:

答案 0 :(得分:1)

可以尝试这个

function changeclass() { if ($('input:checked[title="disable"]').length >= 1 && $('input:checked[title="enable"]').length >= 1) { $('a[name=enable_anchor]').attr('class', 'testdisable'); } else if ($('input:checked[title="enable"]').length >= 1) { $('a[name=enable_anchor]').attr('class', 'testdisable'); } else if ($('input:checked[title="disable"]').length >= 1) { $('a[name=enable_anchor]').attr('class', 'testenable'); } //else { // $('a[name=enable_anchor]').attr('class', 'testdisable'); //} }

答案 1 :(得分:0)

这可以解决您的问题:

    function changeclass() {
alert('before' + $('#testhere').attr('class'));
if ($('input:checked[title="disable"]').length == 1) {
    $('#testhere').attr('class', 'disable');
}
else if (($('input:checked[title="disable"]').length > 1) && ($('input:checked[title="enable"]').length == 1))
{
    $('#testhere').attr('class', 'disable');
}
else if ($('input:checked[title="enable"]').length > 1) {
    $('#testhere').attr('class', 'disable');
}
else {
    $('#testhere').attr('class', 'enable');
}
alert('after' + $('#testhere').attr('class'));}
相关问题