开发者

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>
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜