简体   繁体   中英

Need help in javascript code

var radiobox = document.getElementById('<%=rdoRiskAccepted.ClientID%>');

            alert(radiobox[0].checked);

I am getting undefined as a alert. what am I doing wrong.

getElementById returns a single element because, even for radio groups, the id attribute must be unique . You should use the name attribute to specify a radio group and use getElementsByName instead. For instance:

<input type="radio" name="myRadio" checked><label>1</label>
<input type="radio" name="myRadio"><label>2</label>

JS

var radiobox = document.getElementsByName("myRadio");
alert(radiobox[0].checked);

use alert(radiobox .checked);

I would also like to know why you are using clientid only you have to use <%=this.page.clientid + "_" + rdoRiskAccepted.ClientID%>

With jQuery...

<asp:radiobuttonlist ID="rdoRiskAccepted" runat="server">
    <asp:listitem Value="True" Selected="true">Yes</asp:listitem>
    <asp:listitem Value="False">No</asp:listitem>
</asp:radiobuttonlist>

<script type="text/javascript">
//<![CDATA[
$(document).ready(function(){
    var riskAccepted=$('#<%=rdoRiskAccepted.ClientID%> input');
    alert(riskAccepted[0].checked); //true
});
//]]>
</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