最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Checkuncheck a checkbox in the table row when any checkbox in the same row is clicked - Stack Overflow

programmeradmin0浏览0评论

I have a simple table as following which has checkboxes in the first and last columns of each row.

<table style="width:100%">
  <tr>
    <td><input type="checkbox" /></td>
    <td>Smith</td> 
    <td><input type="checkbox" /></td>
  </tr>
  <tr>
    <td><input type="checkbox" /></td>
    <td>Jackson</td> 
    <td><input type="checkbox" /></td>
  </tr>
</table>

Problem: When I check/uncheck the last column's checkbox in the first row, the first column's checkbox in the same row should be checked/unchecked. Similarly, if I check/uncheck the first column's checkbox, the corresponding last column checkbox should be checked/unchecked.

How can I achieve this in javascript? Any help or pointers would be really appreciated.

Here is the fiddle which I have created: Fiddle

Thank you.

I have a simple table as following which has checkboxes in the first and last columns of each row.

<table style="width:100%">
  <tr>
    <td><input type="checkbox" /></td>
    <td>Smith</td> 
    <td><input type="checkbox" /></td>
  </tr>
  <tr>
    <td><input type="checkbox" /></td>
    <td>Jackson</td> 
    <td><input type="checkbox" /></td>
  </tr>
</table>

Problem: When I check/uncheck the last column's checkbox in the first row, the first column's checkbox in the same row should be checked/unchecked. Similarly, if I check/uncheck the first column's checkbox, the corresponding last column checkbox should be checked/unchecked.

How can I achieve this in javascript? Any help or pointers would be really appreciated.

Here is the fiddle which I have created: Fiddle

Thank you.

Share Improve this question edited Mar 28, 2016 at 16:54 ShellZero asked Mar 28, 2016 at 16:46 ShellZeroShellZero 4,67112 gold badges42 silver badges59 bronze badges 2
  • @RayonDabre I wanted to do it in JavaScript. But sure, jQuery should be great. – ShellZero Commented Mar 28, 2016 at 17:03
  • 1 You may take a look at this Javascript solution and modify the Row's onClick event per your purpose: codeproject./Tips/209416/… – Alexander Bell Commented Mar 28, 2016 at 17:09
Add a ment  | 

2 Answers 2

Reset to default 6

Use :checkbox selector to select input type checkbox elements.

Try this:

$(':checkbox').on('change', function() {
  $(this).closest('tr').find(':checkbox').prop('checked', this.checked);
});
table,
th,
td {
  border: 1px solid black;
  border-collapse: collapse;
}
<script src="https://ajax.googleapis./ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<table style="width:100%">
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Smith</td>
    <td>
      <input type="checkbox" />
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Jackson</td>
    <td>
      <input type="checkbox" />
    </td>
  </tr>
</table>

Using JavaScript:

Use querySelectorAll('[type="checkbox"]') to find checkbox elements.

Try this:

var checkboxes = document.querySelectorAll('[type="checkbox"]');
[].forEach.call(checkboxes, function(checkbox) {
  checkbox.onchange = function() {
    var currentRow = this.parentNode.parentNode;
    var cbElems = currentRow.querySelectorAll('[type="checkbox"]');
    [].forEach.call(cbElems, function(cb) {
      cb.checked = this.checked;
    }.bind(this))
  };
});
table,
th,
td {
  border: 1px solid black;
  border-collapse: collapse;
}
<table style="width:100%">
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Smith</td>
    <td>
      <input type="checkbox" />
    </td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" />
    </td>
    <td>Jackson</td>
    <td>
      <input type="checkbox" />
    </td>
  </tr>
</table>

One possible Javascript solution to toggle Checkboxes on Table Row click is shown below:

HTML

<table id = "Table1">
  <tr>
    <td><input type="checkbox" /></td>
    <td>John Smith</td> 
    <td><input type="checkbox" /></td>
  </tr>
  <tr>
    <td><input type="checkbox" /></td>
    <td>Anna Warner</td> 
    <td><input type="checkbox" /></td>
  </tr>
</table>

CSS

table, th, td{
  border: 1px solid #c0c0c0;
  border-collapse: collapse;
}
table{width:100%;}

Javascript

   // row click will toggle checkboxes
   row_OnClick("Table1")
   function row_OnClick(tblId) {
    try {
        var rows = document.getElementById(tblId).rows;
        for (i = 0; i < rows.length; i++) {
            var _row = rows[i];
            _row.onclick = null;
            _row.onclick = function () {
                return function () {selectRow(this);};
            }(_row);
        }
    }
    catch (err) { }
}
function selectRow(row) {
   row.cells[0].firstChild.checked = !row.cells[0].firstChild.checked;
   row.cells[2].firstChild.checked = row.cells[0].firstChild.checked;
}

Working jsfiddle demo at: https://jsfiddle/t6nsxgnz/ Practical implementation at: http://busny

You can further customize this solution pertinent to your task by modifying the selectRow(row) function:

function selectRow(row) {
   row.cells[0].firstChild.checked = // add your code for the 1st CheckBox
   row.cells[2].firstChild.checked = // add your code for the 2nd CheckBox
}

Another variation of this functionality coded in jQuery can be found in online pop-quiz engine (http://webinfocentral.), implemented via the follwoing code snippet:

// toggle Checkboxes on row click
$(Table1 tr').click(function (event) {

    // find the checkbox in the row
    var _chk = $(this).find('input:checkbox');


    if (!($(event.target).is("checkbox"))) {
        $(_chk).prop('checked', !$(_chk).prop('checked'));
    }
});

In this case, Row Click (at any place of the Row) or CheckBox Click events will toggle the state of that particular CheckBox. The state of other CheckBoxes can be synchronized with this one (by using "siblings" property, for example). Hope this may help.

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论