[英]select input in first td when second td contains word
我有一个带有属性的表,其中每个 tr id 都有一个 attribute_id。 每个 tr 包含 6 个 td,而第一个 td 包含一个输入选择框。
当第三个 td 包含特定单词时,我试图自动化选择输入选择框的过程。 我尝试用 jQuery 做到这一点,但我无法让它工作。 因为我的属性包含所有不同的 id,所以我无法让每个人都工作。
我当前的代码如下所示:
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());
});
});
}
我的 html 代码:
<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>
基本流程是这样的:
tr
tr
是否具有匹配 'attribute_ [number] ' 的id
tr
的第三个td
是否包含“金属”一词checked
设置为true
,否则设置为false
。下面是一个例子:
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.