$(this).is(':checked') 总是 returns false
$(this).is(':checked') always returns false
我有一组复选框,我正在尝试获取单个复选框被单击时的状态。到目前为止,我最好的尝试在 coffeescript 文件中有这个:
if $('.boulderfist').length > 0
unpressed = true
$('.boulderfist').on "click", ->
console.log($(this).is(':checked'))
unpressed = false
盒子是由此视图创建的:
<%= a.collection_check_boxes(:seminar_ids, current_user.seminars, :id, :name) do |b| %>
<tr>
<td width="10%" class="boulderfist"><%= b.check_box %></td>
<td><%= b.label %></td>
</tr>
<% end %>
控制台在每个实例中都打印 "False"。这包括开始时选中的复选框和开始时未选中的复选框。
我也试过了
console.log($(this).prop('checked'))
和
console.log($(this).checked)
和
console.log(this.prop('checked'))
和
console.log(this.checked)
所有其他方法 return 未定义或错误。
感谢您的任何见解。
您正在将事件分配给 <td>
而不是复选框
尝试使用
$('.boulderfist :checkbox')
目标 td 内的复选框 class
我有一组复选框,我正在尝试获取单个复选框被单击时的状态。到目前为止,我最好的尝试在 coffeescript 文件中有这个:
if $('.boulderfist').length > 0
unpressed = true
$('.boulderfist').on "click", ->
console.log($(this).is(':checked'))
unpressed = false
盒子是由此视图创建的:
<%= a.collection_check_boxes(:seminar_ids, current_user.seminars, :id, :name) do |b| %>
<tr>
<td width="10%" class="boulderfist"><%= b.check_box %></td>
<td><%= b.label %></td>
</tr>
<% end %>
控制台在每个实例中都打印 "False"。这包括开始时选中的复选框和开始时未选中的复选框。
我也试过了
console.log($(this).prop('checked'))
和
console.log($(this).checked)
和
console.log(this.prop('checked'))
和
console.log(this.checked)
所有其他方法 return 未定义或错误。
感谢您的任何见解。
您正在将事件分配给 <td>
而不是复选框
尝试使用
$('.boulderfist :checkbox')
目标 td 内的复选框 class