I have some code where I need to check radio buttons, and I know how to do it all client side. Here's the markup:
<input type="radio" id="rbInternalUser" name="radioUserType" value="Internal" onclick="setUserType()"/>
<label for="<%=rbInternalUser.ClientID%>">Internal User</label><br />
<input type="radio" id="rbExternalUser" name="radioUserType" value="External" onclick="setUserType()"/>
<label for="<%=rbExternalUser.ClientID%>">External User</label><br />
And the javascript:
<script type="text/javascript">
function setUserType() {
var userType = $("input[name=radioUserType]:checked").val();
}
</script>
But I need these controls set to runat=server
because I'm doing some code behind stuff as well:
<input type="radio" id="rbInternalUser" name="radioUserType" value="Internal" onclick="setUserType()" runat="server" />
<label for="<%=rbInternalUser.ClientID%>">Internal User</label><br />
<input type="radio" id="rbExternalUser" name="radioUserType" value="External" onclick="setUserType()" runat="server" />
<label for="<%=rbExternalUser.ClientID%>">External User</label><br />
In that case, how do I find out which button is selected? It doesn't seem like the usual $("#<%=myControl.ClientID%>")
will work here. I've tried a few things, and this seems close:
<script type="text/javascript">
function setUserType() {
var radioUserType = $("#<%=rbInternalUser.ClientID%>").prop("name");
var userType = $("input[name=" + radioUserType + "]:checked").val();
}
</script>
radioUserType
is getting the client ID, but then the next line fails, saying, "unrecognized expression: input[name=ctl00$body$EditUser1$radioUserType]:checked".
What do I need to put in place of radioUserType
to make this work? Is this possible? How can you get the name of the radio button group and then check which button from the group is selected?
While selecting elements using css attribute selector, make sure the value you are passing are enclosed in quotes. Example:
$("input[name='John Doe']")
Notice the single quotes around the name 'John Doe'.
Therefore, you need to change the existing statement
var userType = $("input[name=" + radioUserType + "]:checked").val();
to
var userType = $("input[name='" + radioUserType + "']:checked").val();
which will solve your problem.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments