我有一个复选框列表,像这样:检查其点击第一个复选框
<div class="checkbox-list">
<label><input type="checkbox" value="" /> All items</label><br />
<label><input type="checkbox" value="1" /> First item</label><br />
<label><input type="checkbox" value="2" /> Second item</label><br />
<label><input type="checkbox" value="3" /> Third item</label><br />
<label><input type="checkbox" value="4" /> Forth Checkbox</label>
</div>
我找的行为是如果我选择在这个div
(.checkbox-list input[type=checkbox]:first
)所有其他复选框后的第一个复选框被关闭。同样,如果我选择除第一个之外的任何一个,则取消选择第一个。
我在如何检查第一个单击是否正在苦苦挣扎?
$(".checkbox-list").on("change", "input[type=checkbox]", function() {
if ($(this).first()) { // <- not working - how do I know if I clicked the first one?
$("input[type=checkbox]:checked", ".checkbox-list").not(this).prop("checked", false);
$(this).prop("checked", true);
} else {
// Uncheck first checkbox
}
});
这里是一个小提琴,如果你想打转转:http://jsfiddle.net/4c7aubqL/1/
只是一个快速的一个:你复选框动态创建? – 2015-04-06 10:11:38
是的,从ajax填充 – janhartmann 2015-04-06 10:17:24
在那种情况下,我已经做了编辑我的答案,使用正确的'.on()'委托使事件保持活跃状态(就像你一样)。编辑。 – 2015-04-06 10:21:02