简体   繁体   English

document.getelementbyId始终返回null

[英]document.getelementbyId Always returning null

I am having trouble setting panel visibility 我在设置面板可见性时遇到问题

but on change JavaScript returning null reference exception. 但在更改JavaScript时返回null引用异常。

JavaScript runtime error: Unable to get property 'setAttribute' of undefined or null reference JavaScript运行时错误:无法获取未定义或空引用的属性“setAttribute”

错误

I want to make panel TrMarketingDetails visible based on a radio button ( RbMarketing ) change. 我想根据单选按钮( RbMarketing )更改使面板TrMarketingDetails可见。

JavaScript JavaScript的

function trVisible(val) {
    var selected = $("#" + val.id + " input:radio:checked").val();
    if (selected == "1") {
        document.getElementById('<%=TrMarketingDetails.ClientID %>').setAttribute("style", "visibility: visible");
        document.getElementById('<%= hfdMarket.ClientID %>').value = 'Y';
    }
    else if (selected == "2") {
        document.getElementById('<%=TrMarketingDetails.ClientID %>').setAttribute("style", "visibility: hidden");
    }
}

AXPX Code AXPX代码

<tr>
    <td style="font-weight: bold" align="left" class="style4">
        Marketing facilities available
    </td>
    <td style="font-weight: bold" class="style23">
        <asp:RadioButtonList ID="RbMarketing" runat="server" DataTextField="Yes" onchange="trVisible(this);"
            RepeatDirection="Horizontal">
            <asp:ListItem Text="Yes" Value="1"></asp:ListItem>
            <asp:ListItem Text="No" Value="2"></asp:ListItem>
        </asp:RadioButtonList>
        <asp:HiddenField ID="hfdMarket" runat="server" />
        <%--OnSelectedIndexChanged="RbMarketing_SelectedIndexChanged" AutoPostBack="True"--%>
    </td>
</tr>
<asp:Panel ID="TrMarketingDetails" runat="server" Style="visibility: hidden" EnableViewState="true">
    <tr>
        <%--visible="false"--%>
        <td runat="server" style="border: none;" class="style7" align="left">
            Details :
        </td>
        <td runat="server" style="border: none;" class="style26" align="left">
            <asp:TextBox ID="TextMarketingDetails" runat="server" CssClass="textboxCss" autocomplete="off"
                MaxLength="100" Enabled="True" ondrop="return false;" Width="300px" TextMode="MultiLine"></asp:TextBox>
            <asp:RequiredFieldValidator ID="Requiredfieldvalidator49" runat="server" Display="None"
                ControlToValidate="TextMarketingDetails" ValidationGroup="grp1" ForeColor="#F00000"
                SetFocusOnError="true" ErrorMessage="Please enter Marketing details" Enabled="false"></asp:RequiredFieldValidator>
            <asp:ValidatorCalloutExtender ID="ValidatorCalloutExtender3" runat="server" PopupPosition="Right"
                TargetControlID="Requiredfieldvalidator49">
            </asp:ValidatorCalloutExtender>
        </td>
    </tr>
</asp:Panel>

W3C link for style.visibility style.visibility的W3C链接

document.getElementById("controlid").style.visibility = "hidden"

you just change like 你只是改变了

function trVisible(val) {

        var selected = $("#" + val.id + " input:radio:checked").val();
        if (selected == "1") {
            document.getElementById('<%=TrMarketingDetails.ClientID %>').style.visibility = "visible";
            document.getElementById('<%= hfdMarket.ClientID %>').value = 'Y';
        }
        else if (selected == "2") {
            document.getElementById('<%=TrMarketingDetails.ClientID %>').style.visibility = "hidden";
        }
    }

You are already use jquery syntax in your code, then good to do this via jquery syntax like 您已经在代码中使用了jquery语法,然后通过jquery语法这样做很好

$('#<%=TrMarketingDetails.ClientID %>').show();
$('#<%=TrMarketingDetails.ClientID %>').hide();

Check this link with same question 使用相同的问题检查此链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM