How can I determine the SelectedValue of a RadioButtonList in JavaScript?(如何确定 JavaScript 中 RadioButtonList 的 SelectedValue?)
问题描述
我有一个带有数据绑定 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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何确定 JavaScript 中 RadioButtonList 的 SelectedValue?
基础教程推荐
- WPF 模态进度窗口 2022-01-01
- 如果有人提交恶意软件Nuget包怎么办? 2022-01-01
- .NET SerialPort DataReceived 事件未触发 2022-01-01
- C# 从 List<List<int>> 中删除重 2022-01-01
- 如何使用 .Net 检查 Active Directory 服务器是否已启动并正在运行? 2022-01-01
- 我应该在后面的代码中直接使用 Linq To SQL 还是使 2022-01-01
- 当值可以是对象或空数组时反序列化 JSON 2022-01-01
- 禁止输入少量字符,例如'<'、'&a 2022-01-01
- Azure Functions:CosmosDBTrigger 未在 Visual Studio 中触发 2022-01-01
- Moq It.Is<>不匹配 2022-01-01
