JS 给隐藏域赋值 SCRIPT5007: 无法设置未定义或 null 引用的属性"value"
functiondeleteButton(){document.getElementById('divButton').innerHTML="";document.get...
function deleteButton() {
document.getElementById('divButton').innerHTML = "";
document.getElementById('hfButtonName').value = "";
document.getElementById('hfNextNumber').value = "";
document.getElementById('hfButtonCount').value = "0";
}
<div>
<input type="button" id="btnAddButton" name="btnAddButton" value="添加按钮" onclick="return getButton()" />
<input type="button" id="btnDelteButton" name="btnDelteButton" value="删除按钮" onclick="return deleteButton();" />
<div id="divButton" runat="server" style="background: White; height: 100px; width: 500px;">
</div>
<input type="hidden" id="hfButtonName" name="hfButtonName" runat="server" />
<input type="hidden" id="hfNextNumber" name="hfNextNumber" runat="server" />
<input type="hidden" id="hfButtonCount" name="hfButtonCount" runat="server" />
</div> 展开
document.getElementById('divButton').innerHTML = "";
document.getElementById('hfButtonName').value = "";
document.getElementById('hfNextNumber').value = "";
document.getElementById('hfButtonCount').value = "0";
}
<div>
<input type="button" id="btnAddButton" name="btnAddButton" value="添加按钮" onclick="return getButton()" />
<input type="button" id="btnDelteButton" name="btnDelteButton" value="删除按钮" onclick="return deleteButton();" />
<div id="divButton" runat="server" style="background: White; height: 100px; width: 500px;">
</div>
<input type="hidden" id="hfButtonName" name="hfButtonName" runat="server" />
<input type="hidden" id="hfNextNumber" name="hfNextNumber" runat="server" />
<input type="hidden" id="hfButtonCount" name="hfButtonCount" runat="server" />
</div> 展开
2个回答
展开全部
你的隐藏域使用了runat="server",表示此控件由服务器管理,所以它的id可能会经服务器重新分配,若要获取生成html源码后的控件id,可以使用ID.ClientID获取:
function deleteButton(){
document.getElementById('<%=divButton.ClientID%>').innerHTML="";
document.getElementById('<%=hfButtonName.ClientID%>').value="";
document.getElementById('<%=hfNextNumber.ClientID%>').value="";
document.getElementById('<%=hfButtonCount.ClientID%>').value="0";
}
但<%%>符号不能在标记有runat="server"的head标签内出现,所以此代码可放到body标签内或没有runat="server"的head标签内
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询