2016-11-18 66 views
0

我有一个网页,其中有几个<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由网络生成,所以我无法通过ID选择它。我接受javascript和jQuery的答案。

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

+0

你可以用':contains'或'过滤器()'通过任意财产找到一个细胞。有关更多详细信息,请参阅文档:http://api.jquery.com –

+0

尝试$(“tr:first”) –

+0

您能解释您想在复选框中更改什么吗? – Aruna

回答

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

使用'$('td:contains(“13。”)')。eq(0).parent()。find(“:checkbox”)'。获取一个jQuery对象,然后将其转换为DOMElement,然后返回到一个jQuery对象是痛苦的多余的。 –

+0

OP要检查tr的第一个td。它可能会更改为'$('tr> td:contains(“13。”)')'。 – SLePort

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>

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/

相关问题