【发布时间】:2016-04-22 05:47:41
【问题描述】:
我有一个数据表,我试图在其中显示一个复选框,当单击任何行时,它的复选框将被选中
这是我的桌子
<table class="bt-datatable table table-striped" id="bt-user-datatable">
<thead>
<tr>
<th><input type="checkbox" name="select_all" value="1" id="bt-select-all"></th>
<th class="text-left">name</th>
<th class="text-left">gender</th>
<th class="text-left">class</th>
</tr>
</thead>
<tbody>
<%
@user.each do |user|
%>
<tr class="bt-users-row">
<td class="text-left"><input type="checkbox" value="<%= user.id %>"></td>
<td class="text-left"><%= user.name %></td>
<td class="text-left"><%= user.gender %></td>
<td class="text-left"><%= user.class %><div
</tr>
<% end %>
</tbody>
</table>
在javascript中我有
$('.bt-users-row').on('click', function(){
var checkBox = $(this).find('input[type="checkbox"]');
if (checkBox.is(':checked')){
checkBox.prop("checked", false);
}
else if (!checkBox.is(':checked')) {
checkBox.prop("checked", true);
}
});
所以在这我想要做的是当我点击行时它会自动选择受尊重的复选框但是,我有一个问题是当我点击复选框时,它不会检查它
【问题讨论】:
-
可以分享一下生成表的样本吗
-
checkBox.prop("checked", false);这是故意添加到if语句中的吗? -
是的,它被添加以便如果未选中复选框,则应该检查
-
试试这个来检查:
checkBox.prop('checked', 'checked'),这个来取消选中checkBox.prop('checked', '')
标签: javascript jquery html checkbox