【问题标题】:javascript checkbox checked when clicked away单击离开时检查了javascript复选框
【发布时间】: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


【解决方案1】:

问题是,检查默认操作和行检查代码时检查默认操作,否定Action

$('.bt-users-row').on('click', function(e) {
  if ($(e.target).is(':checkbox')) {
    return;
  }
  $(this).find('input[type="checkbox"]').prop('checked', function() {
    return !this.checked;
  });
});

【讨论】:

    猜你喜欢
    • 2011-10-16
    • 2018-08-30
    • 1970-01-01
    • 2012-04-18
    • 1970-01-01
    • 1970-01-01
    • 2015-12-20
    • 2018-10-09
    • 1970-01-01
    相关资源
    最近更新 更多