我正在渲染以下HTML。
<tr class="webgrid-row-style">
<td class="webgrid-column-style">
<span class="activate"><label id="lblMTPrinterID1">MT1</label></span>
<span class="deactivate"><label id="lblMTPrinterID2">MT1</label></span>
</td>
<td class="webgrid-column-style">
<span class="activate"><label id="lblNTPrinterID1">NT1</label></span>
<span class="deactivate"><label id="lblNTPrinterID2">NT1</label></span>
</td>
<td class="webgrid-active-column-style">
<span class="activate selector"><label id="lblActive1">N</label></span>
<span class="deactivate selector"><label id="lblActive2">N</label></span>
</td>
<td class="webgrid-column-style">
<button class="activate edit-user">Activate</button>
<button class="deactivate edit-user">Deactivate</button>
</td>
</tr>
<tr class="webgrid-alternating-row">
<td class="webgrid-column-style">
<span class="activate"><label id="lblMTPrinterID1">MT2</label></span>
<span class="deactivate"><label id="lblMTPrinterID2">MT2</label></span>
</td>
<td class="webgrid-column-style">
<span class="activate"><label id="lblNTPrinterID1">NT2</label></span>
<span class="deactivate"><label id="lblNTPrinterID2">NT2</label></span>
</td>
<td class="webgrid-active-column-style">
<span class="activate selector"><label id="lblActive1">Y</label></span>
<span class="deactivate selector"><label id="lblActive2">Y</label></span>
</td>
<td class="webgrid-column-style">
<button class="activate edit-user">Activate</button>
<button class="deactivate edit-user">Deactivate</button>
</td>
</tr>
我希望使用jQuery遍历每一行,找到标签lblActive1的文本,如果lblActive1为Y,则隐藏行的激活元素。如果lblActive1为N,我将隐藏行的deactivate元素。我有以下jQuery脚本,它隐藏整个表中的activate或deactivate元素,这不是我的意图。我只希望逐行隐藏它们,具体取决于行的lblActive文本。
$('tr.webgrid-row-style').each(function () {
var Active = $("#lblActive1").text();
if (Active == "Y") {
$('.activate').hide();
}
else {
$('.deactivate').hide();
}
});
是的,我喜欢这样做吗?非常感谢。
答案 0 :(得分:2)
尝试 - jQuery“find”方法查找当前选择的后代(即使ID在整个DOM中不唯一)
$(document).ready(function () {
$('tr.webgrid-row-style, tr.webgrid-alternating-row').each(function () {
var Active = $(this).find("#lblActive1").text();
if (Active == "Y") {
$(this).find('.activate').hide();
}
else {
$(this).find('.deactivate').hide();
}
});
});