如何确定在JavaScript中一个单选按钮列表的SelectedValue的? [英] How can I determine the SelectedValue of a RadioButtonList in JavaScript?

查看:77
本文介绍了如何确定在JavaScript中一个单选按钮列表的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中一个单选按钮列表的SelectedValue的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆