简体   繁体   English

如何确定JavaScript中RadioButtonList的SelectedValue?

[英]How can I determine the SelectedValue of a RadioButtonList in JavaScript?

I have an ASP.NET web page with a databound RadioButtonList. 我有一个带有数据绑定RadioButtonList的ASP.NET网页。 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. 我需要通过JavaScript在客户端上确定SelectedValue。 I've tried the following without much luck: 我已经尝试了以下方法,但运气不佳:

var reasonCode = document.getElementById("RadioButtonList1");
var answer = reasonCode.SelectedValue;  

("answer" is being returned as "undefined") Please forgive my JavaScript ignorance, but what am I doing wrong? (“ answer”返回为“ undefined”。)请原谅我对JavaScript的无知,但是我在做什么错呢?

Thanks in advance. 提前致谢。

ASP.NET renders a table and a bunch of other mark-up around the actual radio inputs. ASP.NET在实际的单选输入周围呈现了一个表和许多其他标记。 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);
  }

尝试此操作以从RadioButtonList中获取选定的值。

var selectedvalue = $('#<%= yourRadioButtonList.ClientID %> input:checked').val()

I always View Source. 我总是查看源代码。 You will find each radio button item to have a unique id you can work with and iterate through them to figure out which one is Checked. 您会发现每个单选按钮项都有一个唯一的ID,您可以使用该ID并对其进行迭代以找出被选中的项。

Edit: found an example. 编辑:找到一个例子。 I have a radio button list rbSearch. 我有一个单选按钮列表rbSearch。 This is in an ascx called ReportFilter. 这在一个称为ReportFilter的ascx中。 In View Source I see 在查看源中,我看到了

ReportFilter1_rbSearch_0
ReportFilter1_rbSearch_1
ReportFilter1_rbSearch_2

So you can either loop through document.getElementById("ReportFilter1_rbSearch_" + idx ) or have a switch statement, and see which one has .checked = true. 因此,您可以遍历document.getElementById(“ ReportFilter1_rbSearch_” + idx)或使用switch语句,看看哪个具有.checked = true。

I would like to add the most straightforward solution to this problem: 我想为这个问题添加最直接的解决方案:

var reasons= document.getElementsByName("<%=RadioButtonList1.UniqueID%>");
var answer;
for (var j = 0; j < reasons.length; j++) {
    if (reason[j].checked) {
        answer = reason[j].value;
    }
}

UniqueID is the property that gave you the name of the inputs inside the control, so you can just check them really easily. UniqueID是为您提供控件内输入名称的属性,因此您可以非常轻松地检查它们。

For a 'RadioButtonList with only 2 values 'yes' and 'no', I have done this: 对于只有两个值“ yes”和“ no”的“ RadioButtonList”,我这样做:

var chkval=document.getElemenById("rdnPosition_0")

Here rdnposition_0 refers to the id of the yes ListItem . 在这里rdnposition_0指向yes ListItem的ID。 I got it by viewing the source code of the form. 我是通过查看表单的源代码获得的。

Then I did chkval.checked to know if the value 'Yes' is checked. 然后我做了chkval.checked来知道是否检查了'Yes'值。

RadioButtonList is an ASP.NET server control. RadioButtonList是一个ASP.NET服务器控件。 This renders HTML to the browser that includes the radio button you are trying to manipulate using JavaScript. 这会将HTML呈现给浏览器,其中包括您尝试使用JavaScript操纵的单选按钮。

I'd recommend using something like the IE Developer Toolbar (if you prefer Internet Explorer) or Firebug (if you prefer FireFox) to inspect the HTML output and find the ID of the radio button you want to manipulate in JavaScript. 我建议使用IE开发者工具栏 (如果您喜欢Internet Explorer)或Firebug (如果您喜欢FireFox)之类的东西来检查HTML输出并找到要在JavaScript中操作的单选按钮的ID。

Then you can use document.getElementByID("radioButtonID").checked from JavaScript to find out whether the radio button is selected or not. 然后,您可以使用从JavaScript document.getElementByID("radioButtonID").checked来确定是否选择了单选按钮。

The HTML equivalent to ASP.NET RadioButtonList, is a set of <input type="radio"> with the same name attribute(based on ID property of the RadioButtonList). 与ASP.NET RadioButtonList等效的HTML是一组具有相同名称属性的<input type =“ radio”>(基于RadioButtonList的ID属性)。

You can access this group of radio buttons using getElementsByName. 您可以使用getElementsByName访问这组单选按钮。 This is a collection of radio buttons, accessed through their index. 这是单选按钮的集合,可以通过它们的索引进行访问。

alert( document.getElementsByName('RadioButtonList1') [0].checked );
function CheckRadioListSelectedItem(name) {

    var radioButtons = document.getElementsByName(name);
    var Cells = radioButtons[0].cells.length;

    for (var x = 0; x < Cells; x++) {
        if (document.getElementsByName(name + '_' + x)[0].checked) {
            return x;
        }
    }

    return -1;
}

I've tried various ways of determining a RadioButtonList's SelectedValue in Javascript with no joy. 我已经尝试了各种方法来轻松确定Javascript中RadioButtonList的SelectedValue。 Then I looked at the web page's HTML and realised that ASP.NET renders a RadioButtonList control to a web page as a single-column HTML table! 然后,我查看了网页的HTML,并意识到ASP.NET将Web的RadioButtonList控件作为单列HTML表格呈现给网页!

<table id="rdolst" border="0">
  <tr>
    <td><input id="rdolst_0" type="radio" name="rdolst" value="Option 1" /><label for="rdolst_0">Option 1</label></td>
  </tr>
  <tr>
    <td><input id="rdolst_1" type="radio" name="rdolst" value="Option 2" /><label for="rdolst_1">Option 2</label></td>
  </tr>
</table>

To access an individual ListItem on the RadioButtonList through Javascript, you need to reference it within the cell's child controls (known as nodes in Javascript) on the relevant row. 要通过Javascript访问RadioButtonList上的单个ListItem,您需要在相关行的单元格的子控件(在Javascript中称为节点)内对其进行引用。 Each ListItem is rendered as the first (zero) element in the first (zero) cell on its row. 每个ListItem均呈现为其行上第一个(零)单元格中的第一个(零)元素。

This example loops through the RadioButtonList to display the SelectedValue: 本示例遍历RadioButtonList以显示SelectedValue:

var pos, rdolst;

for (pos = 0; pos < rdolst.rows.length; pos++) {
    if (rdolst.rows[pos].cells[0].childNodes[0].checked) {
        alert(rdolst.rows[pos].cells[0].childNodes[0].value);
        //^ Returns value of selected RadioButton
    }
}

To select the last item in the RadioButtonList, you would do this: 要选择RadioButtonList中的最后一项,请执行以下操作:

rdolst.rows[rdolst.rows.length - 1].cells[0].childNodes[0].checked = true;

So interacting with a RadioButtonList in Javascript is very much like working with a regular table. 因此,与Javascript中的RadioButtonList进行交互非常类似于使用常规表。 Like I say I've tried most of the other solutions out there but this is the only one which works for me. 就像我说的那样,我已经尝试了大多数其他解决方案,但这是唯一对我有用的解决方案。

I wanted to execute the ShowShouldWait script only if the Page_ClientValidate was true . 我只想在Page_ClientValidatetrue执行ShowShouldWait脚本。 At the end of the script, the value of b is returned to prevent the postback event in the case it is not valid. 在脚本末尾,返回b的值以防止在无效情况下发生postback事件。

In case anyone is curious, the ShouldShowWait call is used to only show the "please wait" div if the output type selected is "HTML" and not "CSV". 万一有人好奇,如果选择的输出类型是“ HTML”而不是“ CSV”,则ShouldShowWait调用仅用于显示“ please wait” div

onclientclick="var isGood = Page_ClientValidate('vgTrxByCustomerNumber');if(isGood){ShouldShowWait('optTrxByCustomer');} return isGood"

To check the selected index of drop down in JavaScript: 要在JavaScript中检查下拉列表的选定索引,请执行以下操作:

function SaveQuestion() {
    var ddlQues = document.getElementById("<%= ddlQuestion.ClientID %>");
    var ddlSubQues = document.getElementById("<%=ddlSecondaryQuestion.ClientID%>");

    if (ddlQues.value != "" && ddlSubQues.value != "") {
        if (ddlQues.options[ddlQues.selectedIndex].index != 0 ||
            ddlSubQues.options[ddlSubQues.selectedIndex].index != 0) {
            return true;
        } else {
            return false;
        }
    } else {
        alert("Please select the Question or Sub Question.");
        return false;
    }
}
reasonCode.options[reasonCode.selectedIndex].value

From here : 这里

if (RadioButtonList1.SelectedIndex > -1) 
{
    Label1.Text = "You selected: " + RadioButtonList1.SelectedItem.Text;
}

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

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