简体   繁体   中英

Checkbox won't change value with javascript

I have 2 check boxes where only one or none may be checked. Since I can't do a postback I tried this with Javascript. The Javascript finds the element (tested it with an alert). But the value won't change.

Any Idea how I can do this with Javascript?

The Javascript:

function mrcAndNbbFilterChanged(mrcOrNbb)
        {
            alert("er in");
          if(mrcOrNbb == 0)
          { 
            document.getElementById("ctl00_contentHolder_cb_mrcFilter").checked=true; 
            document.getElementById("ctl00_contentHolder_cbNoBackBilling").checked=false; 
            alert(document.getElementById("ctl00_contentHolder_cbNoBackBilling"));
            alert("0");
          }
          else
          {
            if(mrcOrNbb == 1)
            {
                alert("1");
                document.getElementById("cb_mrcFilter").checked=false; 
                document.getElementById("cbNoBackBilling").checked=true; 
            }
          }
        }

The ASP code:

<asp:CheckBox ID="cb_mrcFilter" runat="server" Text="Only MRC" OnClick="mrcAndNbbFilterChanged(0)" /> 
<asp:CheckBox ID="cbNoBackBilling" runat="server" Text="No back billing" OnClick="mrcAndNbbFilterChanged(1)"  />    

ASP.NET applies that ctl00_ -esque string to IDs when using ASP controls to ensure they are unique. You can get the ASP-modified ID value using:

document.getElementById("<%= cb_mrcFilter.ClientID %>").checked=false;
document.getElementById("<%= cbNoBackBilling.ClientID %>").checked=true;

Also, as a side note, you can use else if { ... } , rather than else { if { ... } } when only dealing with one alternative:

if(mrcOrNbb == 0) {
    ...
}
else if(mrcOrNbb == 1) {
    ...
}

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