以下代碼向調用節點添加複選框和標籤。
我的問題在於label.click函數。每當標籤被點擊時,我想改變匹配複選框的狀態。然而,會發生什麼情況是複選框總是未選中。 爲了調試目的,我現在總是明確地將其設置爲檢查。爲什麼我的複選框總是被取消選中?
當我通過螢火蟲的代碼進行檢查時,我看到複選框被選中,然後離開函數時它會再次被取消選中。
任何想法?
jQuery.fn.AddEndOrStartWith = function(selected, id, action) {
var checkBox = $('<input type="checkbox"></input>');
checkBox.attr("id", id + action);
checkBox.addClass(action + "CheckBox");
checkBox.attr("for", id);
var label = $('<label></label>');
label.attr("for", id + action);
if (selected) {
checkBox.attr("checked", "checked");
label.addClass("lockerClosed");
} else {
label.addClass("lockerOpen");
}
$(this).append(label);
$(this).append(checkBox);
label.click(function() {
/*alert(checkBox.attr("checked"));*/
checkBox.attr("checked", "checked");
/*return true;*/
/*alert(checkBox.attr("checked"));*/
});
}
但是你的標籤設置爲複選框,這樣你就不會需要檢查或標籤上點擊時取消它 - 瀏覽器會爲你。或者我錯過了什麼? – algiecas 2009-07-16 12:05:54
好吧,還有更多事情正在進行,但這會超出問題的範圍。不過你是對的。 – 2009-07-16 12:10:58