【发布时间】:2018-06-14 08:17:13
【问题描述】:
我有以下 JS/jQuery,它获取 ASP DetailsView 控件(呈现 HTML)的单元格值,检查它的条件,并根据所述条件隐藏 div。它基本上是检查单元格值是否像 IP 地址一样格式化......
$(document).ready(function () {
//Grab innerHTML in the IPAddress cell from DetailsView1
//See if it contains the characters: 10.
//If it does not contain '10.', hide the parent div for that user
var ip = document.getElementById("DetailsView1").rows[3].cells[0].innerHTML;
var addrFormat = ip.includes("10.");
if (addrFormat == false) {
$("#IDofDetailsView1ParentDiv").hide();
}
});
这很好用,但我需要检查几个 DetailsView 控件,并可能按 ID .hide() 几个元素。
在 JS/jQuery 中,有没有办法:
ForEach 渲染 HTML 元素,如 DetailsView,检查格式条件,然后仅隐藏此控件的父 div,如果条件为 false
感谢您的建议。
已编辑以显示标记
这是我正在使用的渲染 HTML...
<div class="square" id="myId">
<div class="content">
<div><p id="myId2">Unavailable for Dispatch</p>
<div class="table">
<div class="table-cell">
<div id="UpdatePanel1">
<div>
<table class="Grid" cellspacing="0" rules="all" border="1" id="DetailsView1" style="border-collapse:collapse;">
<tr align="center">
<td colspan="2" style="border-style:None;">Text1</td>
</tr><tr align="center">
<td class="building" colspan="2" style="border-style:None;">Text2</td>
</tr><tr align="center">
<td colspan="2" style="border-style:None;">Text3</td>
</tr><tr align="center">
<td class="hide" colspan="2" style="border-style:None;">Text4</td>
</tr><tr align="center">
<td class="hide" colspan="2">Text5</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
我的页面上有几个。现在我正在使用:
var ip = document.getElementById("DetailsView1").rows[3].cells[0].innerHTML;
但我可能需要在“网格”上使用类选择器来一次处理它们,对吗?
然后我需要按 id 隐藏最高的 div,而不是最近的父级。但只隐藏那些 td 'Text 4' 值为 false 的 div。如果我改用 Grid 类,我不确定如何获取当前从“DetailsView1”获取的 innerHTML。
再次感谢...
【问题讨论】:
-
也许this 可以提供帮助
-
$(elementsSelector).filter(function(){ //return true if their format is invalid }).closest(parentSelector).hide() -
大多数 jQuery.fn 方法对它们操作的 jQuery 对象的结果堆栈中的每个方法执行一个隐式处理。
标签: javascript jquery asp.net foreach innerhtml