【问题标题】:How to check and Uncheck checkbox and also how to make the checkbox click out of the row click如何选中和取消选中复选框以及如何使复选框单击行外单击
【发布时间】:2017-05-08 16:46:39
【问题描述】:

只有一行具有 highlightRowSelected 类,并且该行的复选框被勾选 - 这是有效的

如何使其他复选框取消选择没有类 highlightRowSelected 的其他复选框,也可以勾选其他行复选框,但不应将类 highlightRowSelected 添加到该行,仅应添加行单击(不是来自复选框 col)类highlightRowSelected 到该行

您可以更改函数声明并进行纯 js 调用,而不是在 html 中指定 getdetails(row).. 此外,这些行是动态的,因此无法硬编码 id 或 html 中的内容

看看这个小提琴:https://jsfiddle.net/y7jqb5hp/13/

function getdetails(row) {
  el = window.event.srcElement;
  if (el.id.substr(0, 7) == 'eachRow')
    el = null;
  if (row.cells[0].children[0].checked == false && el != null) {
    row.cells[0].children[0].checked = true;
  } else if (row.cells[0].children[0].checked == false && el != null) {
    row.cells[0].children[0].checked = true;
  }

  $("#tableID tbody tr").each(function() {
    $(this).removeClass("highlightRowSelected");
  });
  
  $(row).addClass("highlightRowSelected");
}
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td,
th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

.highlightRowSelected {
  background-color: #e2e2e2;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tableID">
  <tr onclick="getdetails(this)">
    <th>checkbox</th>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr onclick="getdetails(this)">
    <td><input name="eachRow" type="checkbox" /> </td>
    <td>Alfreds </td>
    <td>Maria </td>
    <td>Germany</td>
  </tr>
  <tr onclick="getdetails(this)">
    <td><input name="eachRow" type="checkbox" /> </td>
    <td>Centro </td>
    <td>Francisco </td>
    <td>Mexico</td>
  </tr>
  <tr onclick="getdetails(this)">
    <td><input name="eachRow" type="checkbox" /> </td>
    <td>Ernst </td>
    <td>Roland </td>
    <td>Austria</td>
</table>

【问题讨论】:

  • 您的代码似乎已经在执行您需要的操作。该复选框在单击的行上被选中,并且仅突出显示最后单击的行。如果这不是您想要的,请更清楚地描述问题和您想要的行为
  • 编辑了描述
  • 你应该使用单选按钮而不是复选框...
  • 它必须是一个 chkecbox -- 抱歉要求不能改变

标签: javascript jquery html css checkbox


【解决方案1】:

我刚刚更改了你的JS代码的第二个条件,添加到unselect others:

$('input:checkbox').removeAttr('checked');

并从标题行中删除了电话getdetails

还添加了:

$('input:checkbox').click(function(e) {
    e.stopPropagation();
});

将复选框勾选与行点击分开处理。

function getdetails(row) {
  el = window.event.srcElement;
  if (el.id.substr(0, 7) == 'eachRow')
    el = null;
  if (row.cells[0].children[0].checked == false && el != null) {
    $('input:checkbox').removeAttr('checked');
    row.cells[0].children[0].checked = true;
  } else if (row.cells[0].children[0].checked == true && el != null) {
    row.cells[0].children[0].checked = false;
  }

  $("#tableID tbody tr").each(function() {
    $(this).removeClass("highlightRowSelected");
  });
  
  $(row).addClass("highlightRowSelected");
}

$('input:checkbox').click(function(e) {
    e.stopPropagation();
});
table {
  font-family: arial, sans-serif;
  border-collapse: collapse;
  width: 100%;
}

td,
th {
  border: 1px solid #dddddd;
  text-align: left;
  padding: 8px;
}

.highlightRowSelected {
  background-color: #e2e2e2;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="tableID">
  <tr>
    <th>checkbox</th>
    <th>Company</th>
    <th>Contact</th>
    <th>Country</th>
  </tr>
  <tr onclick="getdetails(this)">
    <td><input name="eachRow" type="checkbox" /> </td>
    <td>Alfreds </td>
    <td>Maria </td>
    <td>Germany</td>
  </tr>
  <tr onclick="getdetails(this)">
    <td><input name="eachRow" type="checkbox" /> </td>
    <td>Centro </td>
    <td>Francisco </td>
    <td>Mexico</td>
  </tr>
  <tr onclick="getdetails(this)">
    <td><input name="eachRow" type="checkbox" /> </td>
    <td>Ernst </td>
    <td>Roland </td>
    <td>Austria</td>
</table>

【讨论】:

  • 对于其他没有附加类的行需要单击两次以取消选中复选框
  • 我添加了取消选中其他人
  • 取消选中正在工作,但是当单击其他行的复选框时,它添加了类并且复选框没有被勾选。 --- 它应该正好相反
  • 点击其他行复选框时应该发生的情况是它不应该添加类,它应该勾选复选框
  • 你能把这个倒过来吗
【解决方案2】:
use jquery to trigger the function,

ex.

<tr id="1" onclick="checkMe(this.id)"><input type='checkbox'></tr>
<tr id="2" onclick="checkMe(this.id)"><input type='checkbox'></tr>

jquery:

function checkMe(id){
        $(this).closest('[type=checkbox]').attr('checked', true);
}

【讨论】:

    【解决方案3】:

    更新你的 js 函数:

    function getdetails(row) {
    
       if ($(row).find('input[type="checkbox"]').is(":checked")) {
         $(row).find('input[type="checkbox"]').prop('checked', false);
         $(row).removeClass("highlightRowSelected");
         return false;
       }
    
       var isChecked = false;
       $("#tableID tbody tr").each(function () {
         if ($(this).find('input[type="checkbox"]').is(":checked")) {
            isChecked = true;
         }
       });
    
       if (isChecked) {
         $(row).find('input[type="checkbox"]').prop('checked', true);
         return false;
       } else {
         $('table input[type="checkbox"]').prop('checked', false);
         $(row).find('input[type="checkbox"]').prop('checked', true);
       }
    
       $("#tableID tbody tr").each(function () {
         $(this).removeClass("highlightRowSelected");
       });
    
       $(row).addClass("highlightRowSelected");
    }
    

    更新fiddle

    【讨论】:

    • How to make other checkboxes deselect which without class highlightRowSelected -- 这很好用,但可以勾选其他行复选框,但不应将类 highlightRowSelected 添加到该行或复选框不应该去点击函数
    • 没什么不同 - 对不起,如果我让你感到困惑。只有单击复选框才应勾选复选框,现在当我单击其他行而不添加类时,它会勾选其他行的复选框。现在,如果一排正在上课,那么其他行就再也不会上课了。
    • 应该发生的是,当点击其他行的复选框时,它不应该添加类,它应该勾选复选框
    • 哦,那么我们也需要在复选框上添加点击事件。
    猜你喜欢
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-14
    • 1970-01-01
    • 1970-01-01
    • 2018-09-01
    • 1970-01-01
    相关资源
    最近更新 更多