简体   繁体   English

在JavaScript代码中需要帮助

[英]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 . getElementById返回单个元素,因为即使对于无线电组, id属性也必须是唯一的 You should use the name attribute to specify a radio group and use getElementsByName instead. 您应该使用name属性指定一个单选组,而改用getElementsByName For instance: 例如:

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

JS JS

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

use alert(radiobox .checked); 使用alert(radiobox .checked);

I would also like to know why you are using clientid only you have to use <%=this.page.clientid + "_" + rdoRiskAccepted.ClientID%> 我也想知道为什么只使用clientid,而必须使用<%= this.page.clientid +“ _” + rdoRiskAccepted.ClientID%>

With jQuery... 使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM