如何更改另一个元素中的元素

时间:2016-11-18 09:19:03

标签: javascript jquery

我有一个包含多个<tr>元素的页面,其中包含三个内容:

  • <td>背后有数字和点(如下所示:<td>19.</td>
  • <td>,说明为
  • <td>带复选框
<table>
  <!-- Some other <tr>s there... -->
  <tr id="somethingGeneratedWhichIDontKnow">
    <td id="aduju1j">13.</td>
    <td id="ajdi1">lorem ipsum dolor sit amet consectetur adipiscing elit</td>
    <td id="3j21">
      <input type="checkbox" id="3125ac_a">
    </td>
  </tr>
  <!-- Some other <tr>s there... -->
</table>

我需要查找<tr>元素,其中包含一些<td>个数字并更改其中的复选框。

所以我的问题是:如何找到正确的复选框?元素的ID由web生成,因此我无法通过ID选择它。我同时接受javascript和jQuery的答案。

我是JS和jQuery的新手,所以感谢你的帮助: - )

3 个答案:

答案 0 :(得分:2)

var checkbox = $('td:contains("13.")').eq(0).parent().find(":checkbox")

答案 1 :(得分:0)

找到tr,其中第一个td包含一个数字,然后使用tr作为查找checkbox的基础。:

// find the tr
var $tr = $('table tr').filter(function(){
    return $(this).find('td:first').text().match("[0-9]+");
}).first();

// find the checkbox inside a td of the found tr
var $cb = $tr.find("td :checkbox");
console.log($cb.attr('id'));

//here $cb is the checkbox
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
  <!-- Some other <tr>s there... -->
  <tr id="somethingGeneratedWhichIDontKnow">
    <td id="aduju1j">13.</td>
    <td id="ajdi1">lorem ipsum dolor sit amet consectetur adipiscing elit</td>
    <td id="3j21">
      <input type="checkbox" id="3125ac_a">
    </td>
  </tr>
  <!-- Some other <tr>s there... -->
</table>

答案 2 :(得分:0)

怎么样:

var chkList = $('#tbl tr').map(function(t){
    return {
        num: $(this).children('td:first').text(),
        chk: $(this).find(':checkbox')
    }
}).get();

console.log('checkboxesList', chkList);

小提琴: https://jsfiddle.net/zpavrgeo/