我正在获取JavaScript
运行以下代码时出现运行时错误:无法获得属性'样式'未定义或null 参考
错误
它的部分是lblSatisfyQuarterEndDate.style.display = "none";
如果我删除那部分它运行正常。我想让文本框和标签消失然后值为False。
<div class="tdControl col-xl-6 nowrap">
<asp:DropDownList ID="ddlSatisfyQuarterEnd_ID" runat="server" onchange="dd_changed()" ClientIDMode="Static" >
<asp:ListItem Selected="True" Value="-1">-- Select one --</asp:ListItem>
<asp:ListItem Value="False">No</asp:ListItem>
<asp:ListItem Value="True">Yes</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="ddlSatisfyQuarterEnd_ID" CssClass="ValidatorFailedImage" Display="Dynamic" ToolTip="Is Required" InitialValue ="-1" />
</div>
</div>
<div class="col-xl-4 col-lg-4">
<div class="tdLabel col-xl-6 nowrap">
<asp:label ID="lblSatisfyQuarterEndDate" runat="server" Text=" Satisfy Quarter End Requirement Date:" />
</div>
<div class="tdControl col-xl-6 nowrap">
</div>
var ddlSatisfyQuarterEnd_ID = document.getElementById("ddlSatisfyQuarterEnd_ID").value;
var ddlSatisfyQuarterEndDate = document.getElementById("ddlSatisfyQuarterEndDate");
var lblSatisfyQuarterEndDate = document.getElementById("lblSatisfyQuarterEndDate");
function dd_changed() {
var ddlSatisfyQuarterEnd_ID = document.getElementById("ddlSatisfyQuarterEnd_ID").value;
switch (ddlSatisfyQuarterEnd_ID) {
case "True":
ddlSatisfyQuarterEndDate.style.display = "inline";
break;
case "False":
ddlSatisfyQuarterEndDate.style.display = "none";
lblSatisfyQuarterEndDate.style.display = "none";
break;
default:
ddlSatisfyQuarterEndDate.style.display = "none";
答案 0 :(得分:0)
永远不要相信你在.aspx标记中看到的ID。查看浏览器中生成的HTML代码 您还可以改善服务器端的情境绑定ID。这是一个例子:
var ddlSatisfyQuarterEnd_ID = document.getElementById('<%#ddlSatisfyQuarterEnd_ID.ClientID%>').value;
var ddlSatisfyQuarterEndDate = document.getElementById('<%#ddlSatisfyQuarterEndDate.ClientID%>');
var lblSatisfyQuarterEndDate = document.getElementById('<%#lblSatisfyQuarterEndDate.ClientID%>');