我有一个 ASP 表单中的单个复选框列表:
<asp:UpdatePanel runat="server" ID="FiltersUpdPnl">
<ContentTemplate>
<div class="filters">
Show:
<asp:DropDownList runat="server" ID="CapNumProjectsDDL" AutoPostBack="true" OnSelectedIndexChanged="GenericFiltersChanged" >
<%--<asp:ListItem Value="0" Text="" Selected="True"></asp:ListItem>--%>
<asp:ListItem Value="1" Text="Capacity"></asp:ListItem>
<asp:ListItem Value="2" Text="Number of Projects"></asp:ListItem>
</asp:DropDownList>
</div>
<div id="filterlist" class="filters">
<span style="font-size:13pt;display:none;">Filters:<asp:Button runat="server"
ID="ApplyFilters1Btn" Text="Apply New Filters" Visible="false" OnClick="ApplyFilters" /> </span>
<br />
<span style="font-size:10pt;">Project Type:</span>
<ul>
<li>
<asp:CheckBox AutoPostBack="true" runat="server" ID="ShowAllChkBx" Text="(check/uncheck all)"
Checked="false" oncheckedchanged="ShowAllChkBx_CheckedChanged" />
</li>
</ul>
<div id="filterchks" runat="server">
<ul>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="BiomassChkBx" Text="Biomass" Checked="true" Value="7" OnClick="JavaScript:ParentCheck(parent, 'Biomasschks')"/></li>
<div runat="server" id="Biomasschks">
<ul>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="BiomassUnspChkBx" Text="Biomass" Checked="true" Value="18" OnClick="JavaScript:uncheckParent(this,'BiomassChkBx')"/></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="BiomassLandfillGasChkBx" Text="Biomass - Landfill Gas" Checked="true" Value="29" OnClick="JavaScript:uncheckParent(this,'BiomassChkBx')"/></li>
</ul>
</div>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="CSPChkBx" Text="CSP" Checked="true" Value="11" OnClick="JavaScript:ParentCheck(parent, 'CSPchks')" /></li>
<ul>
<div runat="server" id="CSPchks">
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="CheckBox2" Text="CSP" Checked="true" Value="5" OnClick="JavaScript:uncheckParent(this,'CSPChkBx')" /></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="CSPTroughChkBx" Text="CSP - Trough" Checked="true" Value="35" OnClick="JavaScript:uncheckParent(this,'CSPChkBx')" /></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="CSPLensChkBx" Text="CSP - Lens" Checked="true" Value="36" OnClick="JavaScript:uncheckParent(this,'CSPChkBx')" /></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="CSPTowerChkBx" Text="CSP - Tower" Checked="true" Value="37" OnClick="JavaScript:uncheckParent(this,'CSPChkBx')" /></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="CSPDishEngineChkBx" Text="CSP - Dish Engine" Checked="true" Value="38" OnClick="JavaScript:uncheckParent(this,'CSPChkBx')" /></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="CSPLinearFresnelChkBx" Text="CSP - Linear Fresnel" Checked="true" Value="39" OnClick="JavaScript:uncheckParent(this,'CSPChkBx')" /></li>
</div>
</ul>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="GeothermalChkBx" Text="Geothermal" Checked="true" Value="8"/></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="HydroChkBx" Text="Hydro" Checked="true" Value="13"/></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="OceanChkBx" Text="Ocean" Checked="true" Value="9" OnClick="JavaScript:ParentCheck(parent, 'OceanChks')"/></li>
<ul>
<div runat="server" id="OceanChks">
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="CheckBox1" Text="Ocean" Checked="true" Value="3" OnClick="JavaScript:uncheckParent(this,'OceanChkBx')"/></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="OceanWaveChkBx" Text="Ocean - Wave" Checked="true" Value="45" OnClick="JavaScript:uncheckParent(this,'OceanChkBx')" /></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="OceanTidalCurrentChkBx" Text="Ocean - Tidal/Current" Checked="true" Value="46" OnClick="JavaScript:uncheckParent(this,'OceanChkBx')" /></li>
</div>
</ul>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="SolarPVChkBx" Text="Solar PV" Checked="true" Value="10"/></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="WindChkBx" Text="Wind" Checked="true" Value="12" OnClick="JavaScript:ParentCheck(parent, 'WindChks')"/></li>
<ul>
<div runat="server" id="WindChks">
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="WindOnshoreChkBx" Text="Wind - Onshore" Checked="true" Value="6" OnClick="JavaScript:uncheckParent(this,'WindChkBx')" /></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" ID="WindOffshoreChkBx" Text="Wind - Offshore" Checked="true" Value="21" OnClick="JavaScript:uncheckParent(this,'WindChkBx')" /></li>
</div>
</ul>
</ul>
</div>
</div>
<div runat="server" id="StatusDiv" class="filters">
<span style="font-size:10pt;">Status:</span>
<ul>
<li><asp:CheckBox AutoPostBack="true" runat="server" Value="1" ID="OperatingChkBx" Text="Operating" Checked="true" OnCheckedChanged="GenericFiltersChanged" /></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" Value="2" ID="UnderConstructionChkBx" Text="Under Construction" Checked="true" OnCheckedChanged="GenericFiltersChanged" /></li>
<li><asp:CheckBox AutoPostBack="true" runat="server" Value="3" ID="PreConstructionChkBx" Text="Pre-Construction" Checked="true" OnCheckedChanged="GenericFiltersChanged" /></li>
</ul>
<br />
</div>
</ContentTemplate>
</asp:UpdatePanel>
“UncheckParent”功能正常工作,但是当父级被选中/取消选中时,它应该将其相应 div 内的所有复选框更改为其选中状态(即父级选中,子级选中,反之亦然):
function uncheckParent(checkbox, parentcheckboxid) {
var Parentcheckbox = document.getElementById(parentcheckboxid);
if (!checkbox.checked) {
Parentcheckbox.checked = false;
}
}
function ParentCheck(parent, aId) {
if (parent.checked == false) {
checkByParent(aId, false);
alert("false");
}
else if (parent.checked == true) {
checkByParent(aId, true);
}
}
function checkByParent(aId, aChecked) {
var collection = document.getElementById(aId).getElementsByTagName('INPUT');
for (var x = 0; x < collection.length; x++) {
if (collection[x].type.toUpperCase() == 'CHECKBOX')
collection[x].checked = aChecked;
}
}
按照编码,该函数始终返回父复选框的选中属性 == false,正如“错误”警报所证明的那样(仅用于调试)。
为什么无论父复选框的选中属性处于什么状态,这都会返回 false?我尝试从所有复选框中删除 "Checked="true"" 属性,看看这是否是问题所在,但是,功能保持不变。
(起初我有这个编码的服务器端,效果很好,但我不得不重新排列页面并更改回发事件,并且服务器在事件处理程序有机会触发之前读取 Page_Load 上的选中属性。)