簡體   English   中英

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

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

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM