如何使用jQuery以selected类作为表的第二列的目标
我的代码有问题,我想用SELECTED类获取2列表数据的值 首先需要单击第一个表的表行以获取值并添加所选的类。我的函数是这样的 现在,第一个表和单击的表行有一个selected类,通过单击第二个表,我想确定表数据的值是多少,它有一个selected类 注意:我想获得表的第二列,其中包含选定的表数据类 现在,在我的第二个函数中,我想提醒selected类的值如何使用jQuery以selected类作为表的第二列的目标,jquery,Jquery,我的代码有问题,我想用SELECTED类获取2列表数据的值 首先需要单击第一个表的表行以获取值并添加所选的类。我的函数是这样的 现在,第一个表和单击的表行有一个selected类,通过单击第二个表,我想确定表数据的值是多少,它有一个selected类 注意:我想获得表的第二列,其中包含选定的表数据类 现在,在我的第二个函数中,我想提醒selected类的值 $("table#edit_table_chaining_condiments tr").click(function(e){
$("table#edit_table_chaining_condiments tr").click(function(e){
var x = $('table#edit_chainingBuild tbody tr.clickable-row td.clickable-row-condiments.selected td:nth-child(2)').text();
alert(x);
});
我的Html:
<div class="modal fade" id="EditchainingBuilderModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalCenterTitle" aria-hidden="true" data-keyboard="false" data-backdrop="static">
<div class="modal-dialog modal-lg role="document" style="float:right; height:700px; width:490px; ">
<div class="modal-content">
<div class="modal-header" style="background: linear-gradient(-30deg, #00e4d0, #5983e8); color:white;">
<h5 class="modal-title edit_noun_build_item" id="exampleModalLongTitle" style="color:white;"></h5>
<button type="button" class="close" id="closeBuildChainUpdate" data-dismiss="modal" aria-label="Close" style="">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="container">
<div class="header" style="text-align: center;">
<br>
<h3>Build Your Chain Button</h3>
<label>This button will be served as customers menu.</label><br>
<i class="fab fa-creative-commons-remix" style="font-size:70px;"></i>
<br><br>
<input type="hidden" value="" class="edit_hidden_noun_id" name="">
<table class="table table-hover" id="edit_chainingBuild">
<thead>
<tr style="font-size: 15px;">
<th scope="col">Qty</th>
<th scope="col">Condiments</th>
<th scope="col">Price</th>
</tr>
</thead>
<tbody style="font-size:14px;">
</tbody>
</table>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="edit_build_success_insert btn btn-primary">Build Done</button>
</div>
</div>
</div>
</div>
由于添加了selected类,我已经输出了表行高亮显示的结果
你必须了解选择器是如何工作的 当您有$selector1 selector2时,jQuery会查找selector2,它是selector1的子级 在你的很长的一张中,看它的末尾:
$('table#edit_chainingBuild tbody tr.clickable-row td.clickable-row-condiments.selected td:nth-child(2)')
jQuery正在寻找一个td,它是td的第二个孩子,有可点击的行调味品和选定的
看到错误了吗?除非你在另一个td里面有一些td。。。这将是无效的
所以试着把事情减少一点。。。你太具体了,这会导致错误,因为它很难阅读。你不必列出所有的祖先。。首先以选定的元素为目标:
$('#edit_chainingBuild .selected')
你知道这里有两个选定的元素。。。使用以下方法来瞄准正确的目标:
$('#edit_chainingBuild .selected').eq(1) // That is zero-based!
然后,您可以使用.text
祝你好运
$('#edit_chainingBuild .selected').eq(1) // That is zero-based!