[英]How to change element in another element
I have a page which has several <tr>
elements, which contains three things: 我有一个包含几个<tr>
元素的页面,其中包含三件事:
<td>
with number and dot behind it (like this: <td>19.</td>
) <td>
,其后有数字和点(例如: <td>19.</td>
) <td>
with description <td>
带说明) <td>
with checkbox <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>
I need to find <tr>
element which has 1st <td>
with some number and change the checkbox there. 我需要找到具有一些数字的第一个<td>
<tr>
元素,并在那里更改复选框。
So my question is: How can I find the right checkbox? 所以我的问题是:如何找到正确的复选框? ID of elements is generated by web, so I can't select it by ID. 元素的ID是由网络生成的,因此我无法通过ID进行选择。 I accept both javascript and jQuery answers. 我接受javascript和jQuery答案。
I'm new to JS and jQuery so thanks you for all your help :-) 我是JS和jQuery的新手,所以谢谢您的所有帮助:-)
var checkbox = $('td:contains("13.")').eq(0).parent().find(":checkbox")
Find a tr
where the first td
contains a number, then use that tr
as the basis to find the 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>
How about that: 那个怎么样:
var chkList = $('#tbl tr').map(function(t){
return {
num: $(this).children('td:first').text(),
chk: $(this).find(':checkbox')
}
}).get();
console.log('checkboxesList', chkList);
fiddle: https://jsfiddle.net/zpavrgeo/ 小提琴: https : //jsfiddle.net/zpavrgeo/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.