你能做一個更好的代碼嗎?我需要根據父母檢查/取消選中所有孩子,當孩子被選中時,檢查父母,當所有孩子未選中時取消選中父母。檢查/取消選中父母和孩子
$(".parent").children("input").click(function() {
$(this).parent().siblings("input").attr("checked", this.checked);
});
$(".parent").siblings("input").click(function() {
if (this.checked) {
$(this).siblings("div").children("input").attr("checked", true);
return;
}
var childs = $(this).siblings("div").siblings("input");
for (i = 0; i < childs.length; i++) {
if ($(childs.get(i)).attr("checked"))
return;
}
$(this).parent().children("div").children("input").attr("checked", false);
});
做什麼是「.siblings(...)。兄弟姐妹(...)」?還有:「$(this).parent()。children(...)」可能就是「.siblings(...)」 – nickf 2008-10-02 02:52:40