必须选中特定id的复选框

Checkbox must be checked for particular id

本文关键字:id 复选框      更新时间:2023-09-26

下面是一个jquery代码,当点击复选框时会运行。。。但现在我需要为输入类型分配一个id,这样这个函数应该只适用于那个特定的id,解决方案是什么?

<!-- HTML PART -->
<li><input id='checkbox' type='checkbox' value='1' checked="checked"  />  </li>
<!-- JQUERY PART -->
 $("input[type=checkbox]").click(function(){
 var count=$("input:checked").length;
 if(count==5)
 {
$('input[type=checkbox]').not(':checked').attr("disabled",true);
}
 else
 {
$('input[type=checkbox]').not(':checked').attr("disabled",false);
}
save_skills();
get_skill();
}); 

如果将id="checkbox"添加到所需的复选框标记中,并且不在页面中的其他任何位置使用该id,则可以使用此jQuery仅针对该复选框。

$("#checkbox").click(function(){
    // your code
});

如果我没有被误解的话。

$("input[type=checkbox]").click(function(){
    if (this.id == 'your-custom-id') {
       // your code
    }
});

if ('#your-id').click(function(){
    // your code
});

如果您想要,也可以查看以下代码
HTML代码

<li><input id='checkbox' type='checkbox' value='1'  />  </li>
<li><input id='checkbox1' type='checkbox' value='2'   />  </li> 
<li><input id='checkbox2' type='checkbox' value='3'  />  </li> 
<li><input id='checkbox3' type='checkbox' value='4'   />  </li> 
<li><input id='checkbox4' type='checkbox' value='5' />  </li> 
<div id="count"></div>

和js

$('input[type=checkbox]').each(function() {
    $(this).click(function() {
    if ($(this).attr("id") == "checkbox2") {
        $('#count').html("this is what i want");
    }
    else {
        $(this).attr("disabled", true);
        $('#count').html('No this is not');
    }
});
});

这是演示http://jsfiddle.net/Simplybj/aZ5wv/7/
希望这将在某种程度上帮助您