Javascript无法识别Asp Checkbox Checked Property

时间:2012-11-16 21:45:34

标签: javascript asp.net checkbox

我在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;
    }

}

按照编码,该函数始终返回父复选框'checked property == false,由“false”警告(仅用于调试)证明。

无论父复选框的checked属性处于什么状态,为什么返回false?我尝试从所有复选框中删除“Checked =”true“”属性以查看问题是否存在,但唉,功能保持不变。

(我最初有这个编码服务器端,效果很好,但我不得不重新安排页面并更改回发事件,服务器在事件处理程序有机会之前在Page_Load上读取已检查的属性火。)

1 个答案:

答案 0 :(得分:2)

我将专注于一行,其余的则相似 所以在这一行中你有一些主要问题。

<asp:CheckBox AutoPostBack="true" runat="server" ID="CSPChkBx" Text="CSP" 
 Checked="true" Value="11" OnClick="JavaScript:ParentCheck(parent, 'CSPchks')" />
  1. 你发错了id。要使ID有效,您必须将ClientID作为静态放在asp.net 4上,或者在this函数上发送parent()关键字
  2. 你的javascript无法正常运行,因为你发回帖子,你所做的一切都消失了。所以删除以太网autopostback,以太删除javascript。
  3. asp.net检查控件也是一个带有span的输入。因此onclick可能引用了span,而不是输入控件,你需要查看源代码以了解它的去向。
  4. 所以你需要重新设计你喜欢做的事情。