复选框必须检查特定ID
问题描述:
以下是当单击复选框时将运行的jquery代码......但现在我需要为输入类型指定一个id,以便该函数仅适用于工作对于那个特定的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();
});
答
如果我没有误解。
$("input[type=checkbox]").click(function(){
if (this.id == 'your-custom-id') {
// your code
}
});
或者
if ('#your-id').click(function(){
// your code
});
答
如果添加id="checkbox"
到所需的复选框,标签和不使用该ID的其他任何地方页,那么你可以使用这个jQuery的目标只是复选框。
$("#checkbox").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/
希望这将以某种方式帮助你
为什么不能使用'#checkbox'作为选择器? – 2012-04-03 04:49:28