如何在 JavaScript 中确定 RadioButtonList 的 SelectedValue?

发布于 2024-07-13 21:39:24 字数 332 浏览 3 评论 0原文

我有一个带有数据绑定 RadioButtonList 的 ASP.NET 网页。 我不知道在设计时会渲染多少个单选按钮。 我需要通过 JavaScript 确定客户端上的 SelectedValue。 我尝试了以下操作,但运气不佳:(

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

“答案”被返回为“未定义”) 请原谅我对 JavaScript 的无知,但我做错了什么?

提前致谢。

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;  

("answer" is being returned as "undefined")
Please forgive my JavaScript ignorance, but what am I doing wrong?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

昔梦 2024-07-20 21:39:24

ASP.NET 围绕实际的无线电输入呈现一个表格和一堆其他标记。 以下应该有效:-

 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);
  }

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);
  }
合约呢 2024-07-20 21:39:24

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

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

Try this to get the selected value from the RadioButtonList.

var selectedvalue = $('#<%= yourRadioButtonList.ClientID %> input:checked').val()
青衫负雪 2024-07-20 21:39:24

我总是查看源代码。 您会发现每个单选按钮项目都有一个唯一的 ID,您可以使用它并迭代它们以找出选中了哪一个。

编辑:找到一个例子。 我有一个单选按钮列表 rbSearch。 这是在一个名为 ReportFilter 的 ascx 中。 在“查看源代码”中,我看到

ReportFilter1_rbSearch_0
ReportFilter1_rbSearch_1
ReportFilter1_rbSearch_2

“因此,您可以循环遍历 document.getElementById("ReportFilter1_rbSearch_" + idx ) 或使用 switch 语句,然后查看哪一个具有 .checked = true。

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.

Edit: found an example. I have a radio button list rbSearch. This is in an ascx called ReportFilter. 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.

夏见 2024-07-20 21:39:24

RadioButtonList 是一个 ASP.NET 服务器控件。 这会将 HTML 呈现给浏览器,其中包括您尝试使用 JavaScript 操作的单选按钮。

我建议使用类似 IE 开发人员工具栏(如果您喜欢 Internet Explorer)或 Firebug(如果您喜欢FireFox)来检查 HTML 输出并找到要在 JavaScript 中操作的单选按钮的 ID。

然后,您可以使用 JavaScript 中的 document.getElementByID("radioButtonID").checked 来确定单选按钮是否被选中。

RadioButtonList is an ASP.NET server control. This renders HTML to the browser that includes the radio button you are trying to manipulate using 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.

Then you can use document.getElementByID("radioButtonID").checked from JavaScript to find out whether the radio button is selected or not.

终陌 2024-07-20 21:39:24

HTML 相当于 ASP.NET RadioButtonList,是一组 元素。 具有相同的名称属性(基于 RadioButtonList 的 ID 属性)。

您可以使用 getElementsByName 访问这组单选按钮。
这是单选按钮的集合,可通过其索引访问。

alert( document.getElementsByName('RadioButtonList1') [0].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).

You can access this group of radio buttons using getElementsByName.
This is a collection of radio buttons, accessed through their index.

alert( document.getElementsByName('RadioButtonList1') [0].checked );
仅一夜美梦 2024-07-20 21:39:24
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;
}
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;
}
画骨成沙 2024-07-20 21:39:24

对于只有 2 个值“yes”和“no”的“RadioButtonList”,我这样做了:

var chkval=document.getElemenById("rdnPosition_0")

这里 rdnposition_0 指的是 yes ListItem 的 id。 我是通过查看表单的源代码得到的。

然后我执行 chkval.checked 以了解是否检查了值“Yes”。

For a 'RadioButtonList with only 2 values 'yes' and 'no', I have done this:

var chkval=document.getElemenById("rdnPosition_0")

Here rdnposition_0 refers to the id of the yes ListItem. I got it by viewing the source code of the form.

Then I did chkval.checked to know if the value 'Yes' is checked.

沧笙踏歌 2024-07-20 21:39:24

我想为这个问题添加最直接的解决方案:

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 是为您提供控件内输入名称的属性,因此您可以非常轻松地检查它们。

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.

乄_柒ぐ汐 2024-07-20 21:39:24

我尝试了各种方法来确定 JavaScript 中 RadioButtonList 的 SelectedValue,但没有任何乐趣。 然后我查看了网页的 HTML,发现 ASP.NET 将 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>

要通过 Javascript 访问 RadioButtonList 上的单个 ListItem,您需要在相关行上的单元格子控件(在 Javascript 中称为节点)内引用它。 每个 ListItem 都呈现为其行上第一个(零)单元格中的第一个(零)元素。

此示例循环遍历 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
    }
}

要选择 RadioButtonList 中的最后一项,您可以执行以下操作:

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

因此,在 Javascript 中与 RadioButtonList 进行交互非常类似于使用常规表格。 就像我说的,我已经尝试了大多数其他解决方案,但这是唯一适合我的解决方案。

I've tried various ways of determining a RadioButtonList's SelectedValue in Javascript with no joy. 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!

<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. Each ListItem is rendered as the first (zero) element in the first (zero) cell on its row.

This example loops through the RadioButtonList to display the 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:

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. Like I say I've tried most of the other solutions out there but this is the only one which works for me.

夜清冷一曲。 2024-07-20 21:39:24

我想仅当 Page_ClientValidatetrue 时才执行 ShowShouldWait 脚本。 在脚本末尾,返回 b 的值,以防止在无效的情况下发生postback 事件。

如果有人好奇,如果选择的输出类型是“HTML”而不是“CSV”,则 ShouldShowWait 调用仅用于显示“请稍候”div

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

I wanted to execute the ShowShouldWait script only if the Page_ClientValidate was true. At the end of the script, the value of b is returned to prevent the postback event in the case it is not valid.

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".

onclientclick="var isGood = Page_ClientValidate('vgTrxByCustomerNumber');if(isGood){ShouldShowWait('optTrxByCustomer');} return isGood"
你在我安 2024-07-20 21:39:24

要在 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;
    }
}

To check the selected index of drop down in 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;
    }
}
葬﹪忆之殇 2024-07-20 21:39:24
reasonCode.options[reasonCode.selectedIndex].value
reasonCode.options[reasonCode.selectedIndex].value
北风几吹夏 2024-07-20 21:39:24

来自此处

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

From here:

if (RadioButtonList1.SelectedIndex > -1) 
{
    Label1.Text = "You selected: " + RadioButtonList1.SelectedItem.Text;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文