![](/img/trans.png)
[英]How to change the place of an element in another element by javascript or jQuery?
[英]How to change element in another element
我有一个包含几个<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>
我需要找到具有一些数字的第一个<td>
<tr>
元素,并在那里更改复选框。
所以我的问题是:如何找到正确的复选框? 元素的ID是由网络生成的,因此我无法通过ID进行选择。 我接受javascript和jQuery答案。
我是JS和jQuery的新手,所以谢谢您的所有帮助:-)
var checkbox = $('td:contains("13.")').eq(0).parent().find(":checkbox")
查找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>
那个怎么样:
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/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.