【发布时间】:2018-09-15 17:52:59
【问题描述】:
下面是我尝试在单击按钮时用来选择复选框的 jQuery;但是,单击按钮时,不会选中复选框。单击按钮不会执行任何操作。
用于选择所有复选框的 Jquery
$("#selectAllPubTypes").click(function () {
$(".pubbox").each(function () {
$(this).prop('checked',true);
});
});
#Html for button and checkbox group.i have a group of checkboxes here.
<a class="btn waves-effect waves-light" name="selectAllPubs" id="pubs" style="background-color: #0C2340">All</a>
{% for value, text in form.PUBS %}
<label>
<input type="checkbox" class= "filled-in pubbox" name="pubcheckbox" value={{ text }}/>
<span style="font-size: medium">{{ text }}</span>
</label>
{% endfor %}
【问题讨论】:
-
$("#selectAllPubTypes")选择 ID 为selectAllPubTypes的元素,而您没有发布该元素。你需要发一个minimal reproducible example -
你不需要
.each()。.prop()将自动循环遍历集合中的所有元素。 -
#selectAllPubTypes应该只是#pubs。 -
@j08691 我认为他确实发布了一个 MCVE,并且错误就在其中:选择器中的 ID 与
All链接的 ID 不同。
标签: jquery html user-interface jquery-ui frontend