如何使用Javascript检查页面加载时是否选中了复选框?这些代码都不起作用:
<asp:CheckBox runat="server" ID="chkEmailConsent" Checked='<%# bool.Parse(Eval("EmailConsent").ToString()) %>' Enabled="false" />
document.getElementById("<%= chkEmailConsent.ClientID %>")
document.getElementById("ctl00_cphGlobalContent_FormView1_chkEmailConsentID")
答案 0 :(得分:0)
我喜欢在服务器端和客户端控件上使用相同的名称,所以我添加了ClientIDMode =“Static”。这样我就不必使用&lt;%= stuff来获取控件名。
<asp:CheckBox runat="server" ClientIdMode="Static" ID="chkEmailConsent" Checked='<%# bool.Parse(Eval("EmailConsent").ToString()) %>' Enabled="false" />
<script>
// note, this works only because I check the value after the control.
// otherwise it would need to be in a function or something like that
alert(document.getElementById("chkEmailConsent").checked)
</script>