开发者

Need a javascript to enable or disable the check box

开发者 https://www.devze.com 2023-01-17 05:07 出处:网络
Hi all i am having two check box controls in my gridview what i need is if i checked a check box the othe开发者_开发问答r should be get enabled and if i uncheck it should get disable is there any way

Hi all i am having two check box controls in my gridview what i need is if i checked a check box the othe开发者_开发问答r should be get enabled and if i uncheck it should get disable is there any way to do it.

I write this but i am not getting the required one

 <script type="text/javascript">
  function checkboxClick(checked, boxId) {
 var childCheckbox = document.getElementById(boxId);
 childCheckbox.disabled = !checked;

  if(childCheckbox.disabled) //uncheck when disabled
     childCheckbox.checked = false;
}


</script>

  <asp:CheckBox ID="CheckBox1" runat="server" OnClientClick ="checkboxClick(this.checked);" />
    <asp:CheckBox ID="boxId" runat="server" Enabled="false" /></div>


Basically, what you want is something like this:

function checkboxClick(checked) {
   document.getElementById('childCheckbox').disabled = !checked;
}

<input type="checkbox" onclick="checkboxClick(this.checked);" />
<input type="checkbox" id="childCheckbox" />

Now, if they're ASP.NET controls, <asp:CheckBox />, you will probably want to edit checkboxClick so that it also takes the ClientID of the checkbox to enable/disable.

<asp:CheckBox runat="server" id="cb1" />
<asp:CheckBox runat="server" id="cb2" />

var cb2 = FindControl("cb2");
((CheckBox) FindControl("cb1")).OnClientClick = "checkboxClick(this.checked, '" + cb2.ClientID + "');";

EDIT

Accommodating further requirements as per comments:

function checkboxClick(checked, boxId) {
   var childCheckbox = document.getElementById(boxId);
   childCheckbox.disabled = !checked;

   if(childCheckbox.disabled) //uncheck when disabled
      childCheckbox.checked = false;
}

<asp:CheckBox runat="server" id="cb1" />
<asp:CheckBox runat="server" id="cb2" Enabled="false" /> <!-- disable on load -->
0

精彩评论

暂无评论...
验证码 换一张
取 消