简体   繁体   中英

Enable textbox for a radiobuttonlist one of the listitem value using javascript

script for enable/disable

<script type="text/javascript" language="javascript">
    function display() {
     if (document.getElementById("<%=Institution.ClientID %>").value == "others") {
         document.getElementById('<%= lblSpecify.ClientID %>').style.visiblity = "visible";
         document.getElementById('<%= InstituteOthers.ClientID %>').style.visiblity = "visible";
     }
     else {
         document.getElementById('<%= lblSpecify.ClientID %>').style.visiblity = "hidden";
         document.getElementById('<%= InstituteOthers.ClientID %>').style.visiblity = "hidden";
     }

 }

</script>

If 5th listitem(AnyOther) is selected then only label(lblSpecify) and textbox(InstituteOthers) should be visible=true ,otherwise visible=false

Why did you choose our Institution:

1Doctor 2WordofMouth 3Camps 4OldPatients 5AnyOther

  </td> <td> <asp:Label ID="lblSpecify" runat="server" Text="Specify :" Visible="false"></asp:Label> </td> <td> <asp:TextBox ID="InstituteOthers" runat="server" TabIndex="18" MaxLength="20" Visible="false"></asp:TextBox> </td> </tr> </table> 

But it is not effecting.please help.

i am calling the script in

onchange="display" RepeatDirection="Horizontal" TabIndex="18" >

There is also OnSeletedIndexChanged event exists on radiobuttonlist you can hide and dispaly textboxes on the value selected.

For doing this in javascript try below script

var list = document.getElementById(<%=Institution.ClientID %>);
var inputs = list.getElementsByTagName("input");
 var selected;
 for (var i = 0; i < inputs.length; i++) {
      if (inputs[i].checked) {
          selected = inputs[i];
          break;
       }
  }
  if (selected.value == "others") {
       document.getElementById('<%= lblSpecify.ClientID %>').style.visiblity =   "visible";
       document.getElementById('<%= InstituteOthers.ClientID %>').style.visiblity = "visible";
  }

Can you give the where you are calling display method.

If there is dropdown list on your page you can do this on selectedindex change event.

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