如果根据列名 id 选中复选框,则查找 id 列的值

Find the value of id column if checkbox is checked on the basis of column name id

本文关键字:id 查找 复选框 如果      更新时间:2023-09-26

我必须根据列名在同一行中找到其复选框被选中的 id 列的值,因为 id 列位置不固定。它可以是表中的第一个,最后一个或中间。我应用了以下代码:

<table id="rowclick2" border="1">
    <tr>
        <th>Select</th>
        <th>Des</th>
        <th>ID</th>
    </tr>
    <tr>
        <td>
            <input type="checkbox" name="vehicle" value="Bike">
        </td>
        <td>col 2, row 1</td>
        <td>col 3, row 1</td>
    </tr>
    <tr>
        <td>
            <input type="checkbox" name="vehicle" value="Car"> </td>
        <td>col 2, row 2</td>
        <td>col 3, row 2</td>
    </tr>
</table>

我的jquery是:

$("input[name='vehicle']").click(function() {
    alert("djb");
    if ($(this).is(':checked')) {
        alert("dj");
        if ($(this).closest('table').next('tr').find('th:contains(Jan 3)') == 'true') {
            alert("bhh");
        } else {
            alert("cbjhj");
        }
    }
});

请帮助查找选中复选框的 id 值。我是jquery,javascript的新手。

我建议采用以下方法:

// binding a change event-handler to <input type="checkbox" /> elements:
$('input[type=checkbox]').change(function() {
  // assuming you want this to run only when the checkbox is checked:
  if (this.checked) {
    // find the <th> elements:
    var ID = $('th').filter(function() {
        // find those <th> elements whose trimmed-text is exactly 'ID':
        return $.trim($(this).text()) === 'ID';
      // get the cellIndex property of that element (or the first matched element):
      }).prop('cellIndex'),
      // find the closest <tr> element from the checked <input />:
      IDVal = $(this).closest('tr')
      // find the descendant <td> elements:
      .find('td')
      // find the <td> with the same cellIndex as the <th>
      .eq(ID)
      // retrieve the text:
      .text();
    console.log(IDVal);
  }
});

$('input[type=checkbox]').change(function() {
  if (this.checked) {
    var ID = $('th').filter(function() {
        return $.trim($(this).text()) === 'ID';
      }).prop('cellIndex'),
      IDVal = $(this).closest('tr').find('td').eq(ID).text();
    console.log(IDVal);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="rowclick2" border="1">
  <tr>
    <th>Select</th>
    <th>Des</th>
    <th>ID</th>
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="vehicle" value="Bike">
    </td>
    <td>col 2, row 1</td>
    <td>col 3, row 1</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="vehicle" value="Car">
    </td>
    <td>col 2, row 2</td>
    <td>col 3, row 2</td>
  </tr>
</table>

但是,如果您使用类名来标识哪些单元格包含"id"关联值,例如<td class="id"></td>,这一切都会变得简单得多:

$('input[type=checkbox]').on('change', function() {
  if (this.checked) {
    // find the closest <tr> element from the :checked <input />:
    var idValue = $(this).closest('tr')
      // find the element with the class of 'id':
      .find('.id')
      // retrieve the text of that element:
      .text();
    console.log(idValue);
  }
});

$('input[type=checkbox]').on('change', function() {
  if (this.checked) {
    var idValue = $(this).closest('tr').find('.id').text();
    console.log(idValue);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table id="rowclick2" border="1">
  <tr>
    <th>Select</th>
    <th>Des</th>
    <th class="id">ID</th>
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="vehicle" value="Bike">
    </td>
    <td>col 2, row 1</td>
    <td class="id">col 3, row 1</td>
  </tr>
  <tr>
    <td>
      <input type="checkbox" name="vehicle" value="Car">
    </td>
    <td>col 2, row 2</td>
    <td class="id">col 3, row 2</td>
  </tr>
</table>

引用:

  • JavaScript:
    • cellIndex .
  • j查询:
    • closest() .
    • eq() .
    • filter() .
    • find() .
    • jQuery.trim() .
    • on() .
    • prop() .

我希望这有所帮助。请看演示。

根据我从中了解到的情况,请帮助查找选中复选框的 id 值

$(document).ready(function(){
    $(":checkbox").click(function(){
        alert( $(this).val() );
    });
});

http://jsfiddle.net/silkherb/tx2asmg6/2/

将 ID 添加到复选框id="first"id="second"以获取元素:

    <tr>
    <td><input type="checkbox" name="vehicle" value="Bike" id="first"></td>
    <td>col 2, row 1</td>
    <td>col 3, row 1</td>
</tr>`enter code here`
<tr>
    <td><input type="checkbox" name="vehicle" value="Car" id="second"> </td>
    <td>col 2, row 2</td>
    <td>col 3, row 2</td>
</tr>

之后,您可以使用简单的JavaScript。 .checked检查复选框是否已标记。

if(document.getElementById("first").checked == true){
  console.log("do something here 1");
}
if(document.getElementById("second").checked == true){
  console.log("do something here 2");
}