简体   繁体   English

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

[英]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.

相关问题 如何通过javascript或jQuery更改元素在另一个元素中的位置? - How to change the place of an element in another element by javascript or jQuery? 单击React中的另一个元素后如何将className更改为元素 - How to change className to element after click on another element in React 如何通过使用JavaScript单击另一个元素来更改元素的字体? - How to change the font of an element by clicking on another element with JavaScript? 如何在另一个元素 REACT 上更改元素 onClick 的样式? - How to change style of an element onClick at another element REACT? 如何在js上用另一个根元素属性更改根元素 - How to change root element with another root element property on js 如何根据另一个数组元素更改数组元素 - How to change an array element based on another array element 如何通过使用jQuery单击另一个元素来更改元素的类 - How to change the class of an element by clicking on another element with jQuery 如何用HTML + CSS中的另一个元素选择器更改元素的属性值? - How to change the attribute value of an element with by another element Selector in HTML + CSS? 如何通过将鼠标悬停在另一个元素上来更改div的类 - How to change class of a div by hovering on another element 如何根据另一个元素的类更改元素的类 - How to change the class of an element based on the class of another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM