我正在研究一个项目,我遇到了复选框问题。这是一个邮件订阅功能,通过复选框选中/取消选中所有订阅者。
html代码就像这样:
<div class="select_subscribers">
<div class="everyone">
<label>
<input id="select_all_subscribers" name="select_all_subscribers" type="checkbox">
<strong>Everyone</strong>
</label>
</div>
<div class="select_subscribers panel_for_selection">
<label>
<input id="subscriber_ids_" name="mail[subscriber_ids][]" type="checkbox" value="3">
John
</label>
<label>
<input id="subscriber_ids_" name="mail[subscriber_ids][]" type="checkbox" value="2">
Peter
</label>
</div>
</div>
javascript代码就像这样:
$(".extra .select_subscribers .everyone").delegate "input[type='checkbox']", "click", ->
$(".select_subscribers.panel_for_selection input[type='checkbox']").attr("checked", this.checked)
第一次使用它,检查取消选中,然后它不再起作用。选中“select_all_subscribers”复选框后,订阅者复选框的属性为“checked ='checked'”,但未在页面上检查。
我该怎么办?
答案 0 :(得分:1)
使用.prop()代替.attr()
$(".extra .select_subscribers .everyone").delegate "input[type='checkbox']", "click", ->
$(".select_subscribers.panel_for_selection input[type='checkbox']").prop("checked", this.checked)