问题描述
我有一个带有数据绑定 RadioButtonList 的 ASP.NET 网页.我不知道在设计时会渲染多少单选按钮.我需要通过 JavaScript 确定客户端上的 SelectedValue.我尝试了以下方法,但运气不佳:
I have an ASP.NET web page with a databound RadioButtonList. I do not know how many radio buttons will be rendered at design time. I need to determine the SelectedValue on the client via JavaScript. I've tried the following without much luck:
var reasonCode = document.getElementById("RadioButtonList1");
var answer = reasonCode.SelectedValue;
(答案"被返回为未定义")请原谅我对 JavaScript 的无知,但我做错了什么?
("answer" is being returned as "undefined") Please forgive my JavaScript ignorance, but what am I doing wrong?
提前致谢.
推荐答案
ASP.NET 围绕实际的无线电输入呈现一个表格和一堆其他标记.以下应该有效:-
ASP.NET renders a table and a bunch of other mark-up around the actual radio inputs. The following should work:-
var list = document.getElementById("radios"); //Client ID of the radiolist
var inputs = list.getElementsByTagName("input");
var selected;
for (var i = 0; i < inputs.length; i++) {
if (inputs[i].checked) {
selected = inputs[i];
break;
}
}
if (selected) {
alert(selected.value);
}
这篇关于如何确定 JavaScript 中 RadioButtonList 的 SelectedValue?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!