简体   繁体   中英

select input in first td when second td contains word

I have a table with attributes where every tr id has an attribute_id. each tr contains 6 td's whereas the first td includes an input select box.

I am trying to automate the process of selecting input select boxes when the third td contains a specific word. I tried do this with jQuery but I cant get it work. I could not get the two for eaches work since I have attributes containing all different id's.

My current code looks as follows:

var id = 1;
while(id != 10000){
    jQuery('#attribute_ " + id + " > tbody > tr').each(function(index, value) {
        jQuery('#+"+id+" > tbody > tr').each(function(index, value) {
            $('td:contains("metal")', td[1].select());
        });
    });
}

my html code:

<tr class="combination loaded" id="attribute_7892" data="7892" data-index="7892" style="display: 
table-row;">
  <td width="1%">
                   //select this when contains metal
      <input class="js-combination" type="checkbox" data-id="7892" data-index="7892"> 
  </td>

  <td class="img"><img src="http://image" class="img-responsive"></td>

                   //contains metal:
  <td>Chain - plastic, left - right, skin - metal silver - V6, color - dark/gray</td>
  
  <td class="attribute-price">5.00</td>
</tr>

The basic process would be like this:

  1. Loop through each tr
  2. Check if the tr has an id matching 'attribute_ [number] '
  3. Check if the 3rd td of that tr contains the word 'metal'
  4. Set checked to true if so, otherwise set to false .

Here is an example:

 var search = 'metal'; var rows = $('tr.combination.loaded'); rows.each(function(index, value) { if (/(attribute\\_)(\\d+)/g.test(value.id)) { $(this).children(0).children(0).attr('checked', ($(this).children(2).text().indexOf(search) !== -1)); }; });
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <table> <tr class="combination loaded" id="attribute_7892" data="7892" data-index="7892" style="display: table-row;"> <td width="1%"> //select this when contains metal <input class="js-combination" type="checkbox" data-id="7892" data-index="7892"> </td> <td class="img"><img src="http://image" class="img-responsive"></td> //contains metal: <td>Chain - plastic, left - right, skin - metal silver - V6, color - dark/gray</td> <td class="attribute-price">5.00</td> </tr> </table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM