在.aspx页面上有以下单选按钮列表:
<asp:RadioButtonList ID="rbList" runat="server">
<asp:ListItem Text="I accept" Value="accept" />
<asp:ListItem Text="I decline" Value="decline" Selected="True" />
</asp:asp:RadioButtonList>
默认情况下,选择第二台收音机。是否有一种方法可以确定用户是否还没有选择第一个选项,即在执行操作时仍然选择“拒绝”?
例如:
function checkRbList() {
var rbl = document.getElementById(<%= rbList.ClientID %>);
//if "decline" is still selected, alert('You chose to decline')...
}
发布于 2017-11-29 02:04:59
下列人员应做好这项工作:
var rbl = document.getElementById("<%= rbList.ClientID %>");
var value = rbl.value;
if(value === 'decline')
alert()
发布于 2017-11-29 02:22:49
假设您呈现了这个HTML:
<label>
I accept
<input id="rbList_0" name="rbList" type="radio" value="accept" />
</label>
<label>
I decline
<input id="rbList_1" name="rbList" checked="true" type="radio" value="decline" />
</label>
您可以使用document.getElementsByName()。然后使用:
document.getElementsByName("rbList")
,你会得到一个NodeList。
这是一项功能:
function checkRbList() {
var rbl = document.getElementsByName("rbList"), len = rbl.length;
for (var i = 0; i < len; i++) {
if (rbl[i].checked) { // If checked?
return rbl[i].value; // Returns the selected value.
}
}
}
要检查是否仍然选择了"decline"
:
var targetValue = "decline";
if (checkRbList() === targetValue) {
alert("You chose to decline.");
}
就像这样:
(function() {
var targetValue = "decline";
function checkRbList() {
var rbl = document.getElementsByName("rbList"),
len = rbl.length;
for (var i = 0; i < len; i++) {
if (rbl[i].checked) { // If checked?
return rbl[i].value; // Returns the selected value.
}
}
}
var btnValidate = document.getElementById("btnValidate");
btnValidate.onclick = function() {
console.log(checkRbList()); // Prints the selected value.
if (checkRbList() === targetValue) {
alert("You chose to decline.");
}
};
})();
<label>
I accept
<input id="rbList_0" name="rbList" type="radio" value="accept" />
</label>
<label>
I decline
<input id="rbList_1" name="rbList" checked="true" type="radio" value="decline" />
</label>
<button id="btnValidate" type="button">Validate</button>
发布于 2017-11-29 16:04:33
我找到了一种行之有效的方法:
var targetValue = "decline";
$('#<% = myBtn.ClientID %>').click(function () {
var items = $("#<% = rbList.ClientID %> input:radio");
for (var i = 0; i < items.length; i++) {
if (items[i].value == targetValue) {
if (items[i].checked) {
alert(items[i].value);
}
}
}
});
https://stackoverflow.com/questions/47543804
复制相似问题