简体   繁体   中英

Check if ASPX checkbox is checked with javascript

How can I check if the checkbox is checked or not on page load with Javascript? None of the codes worked:

<asp:CheckBox runat="server" ID="chkEmailConsent" Checked='<%# bool.Parse(Eval("EmailConsent").ToString()) %>' Enabled="false" />


document.getElementById("<%= chkEmailConsent.ClientID %>")

document.getElementById("ctl00_cphGlobalContent_FormView1_chkEmailConsentID")

I like using the same name for my controls both server and client side so I add ClientIDMode="Static". That way I do not have to use the <%= stuff to get the control name.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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