[英]Checkbox in label tag : Different result in Chrome and Firefox
<label for="checkbox2" id="label1">
click <input type="checkbox" id="checkbox1">
</label>
<input type="checkbox" disabled="true" id="checkbox2"/>
$(document).ready(function(){
$('#checkbox1').click(function(event) {
$('#checkbox2').prop('disabled', !this.checked);
});
});
頁面中有兩個復選框。 (#checkbox1,#checkbox2)
#checkbox1在標簽標記中,而#checkbox2在標簽標記中。
我想根據#checkbox1啟用/禁用#checkbox2。
當我選中#checkbox1時,#checkbox2已正確啟用。
但是,僅在Firefox中同時檢查#checkbox2。 (不是IE和Chrome)
我認為問題與事件的排序有關。 但是,我不清楚。
編輯:
刪除for
屬性,然后將事件監聽器從click
change
為change
。
該label
元素for
屬性必須被連接到checkbox1
元素,但你可以在checkbox1的變化處理改變checkbox2的選中狀態
<label for="checkbox1" id="label1">click
<input type="checkbox" id="checkbox1">
</label>
<input type="checkbox" disabled="true" id="checkbox2" />
然后
$(document).ready(function () {
$('#checkbox1').change(function (event) {
$('#checkbox2').prop({
disabled: !this.checked,
checked: this.checked
});
});
});
演示: 小提琴
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.