简体   繁体   English

查找选中的单选按钮的值

[英]to find the value of the radio button checked

hi I have a radio button list inside a repeater , the repeater is inside a Datalist.i need to get the value of the selected radio button. 嗨,我在中继器中有一个单选按钮列表,该中继器在数据列表中。我需要获取所选单选按钮的值。 Also I need to select only a single radio in entire datalist using javascript. 另外,我只需要使用javascript在整个数据列表中选择一个无线电。

I would use the normal HTML Radio Button control. 我会使用普通的HTML单选按钮控件。 Everything else is pretty complicated. 其他一切都很复杂。

Then you can use the following code to figure out which one is selected: 然后,您可以使用以下代码来确定选择了哪个代码:

http://remy.supertext.ch/2008/02/find-checked-radio-button-in-aspnet/ http://remy.supertext.ch/2008/02/find-checked-radio-button-in-aspnet/

You can do it with pure client side JavaScript regardless of Repeater, DataList or anything. 您可以使用纯客户端JavaScript进行操作,而不管Repeater,DataList或其他任何内容。

Have this code in your page: 在您的页面中包含以下代码:

<script type="text/javascript">
function GetSelectedRadioButtonValue(strGroupName) {
    var arrInputs = document.getElementsByTagName("input");
    for (var i = 0; i < arrInputs.length; i++) {
        var oCurInput = arrInputs[i];
        if (oCurInput.type == "radio" && oCurInput.name == strGroupName && oCurInput.checked)
            return oCurInput.value;
    }
    return "";
}
</script>

Then to get the selected value call the function passing the name of the radio buttons group - all radio buttons with same name considered a group and the browser will let the user select only one of these. 然后,要获取选定的值,请调用传递单选按钮组name的函数-具有相同名称的所有单选按钮均视为一个组,浏览器将允许用户仅选择其中一个。

Live test case: http://jsfiddle.net/yahavbr/BL9xJ/ 实时测试案例: http//jsfiddle.net/yahavbr/BL9xJ/

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

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