Javascript javascript中的RadioButtonList选定值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27915523/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 00:53:08  来源:igfitidea点击:

RadioButtonList selected value in javascript

javascriptasp.netradiobuttonlist

提问by eljon_i3

I just want to ask how can I select the value of my radiobuttonlist here is my code.

我只是想问我如何选择我的单选按钮列表的值,这里是我的代码。

    function CheckRadioButton(rdbRemarks){
         document.getElementByID(rdbRemarks).SelectedValue = "1";
     }

Here is the code of my button onclick

这是我的按钮 onclick 的代码

<asp:Button ID="CheckRadioButton" CssClass="ButtonCalibri" Width="80px" runat="server" Text="Save" onclick="CheckRadioButton('<%=rdbRemarks.ClientID%>')" />

Here is the code of my radiobuttonlist in case you need a reference

这是我的单选按钮列表的代码,以防您需要参考

    <asp:RadioButtonList ID="rdbRemarks" CssClass="LabelCalibri" runat="server" onselectedindexchanged="rdbRemarks_SelectedIndexChanged">
<asp:ListItem Value="1" Text="Charge to Utilization"></asp:ListItem>
<asp:ListItem Value="2" Text="Charge to Operating Expense"></asp:ListItem>
</asp:RadioButtonList>

What I want is when I click the button, one of the radio button in the list will be selected, for example the selected value is "1" the first radio button will be selected. I currently have no idea how to do that now, Please help me on this one.

我想要的是当我单击按钮时,将选择列表中的一个单选按钮,例如选定的值为“1”,第一个单选按钮将被选择。我目前不知道该怎么做,请帮助我解决这个问题。

回答by Arunprasanth K V

In your button clickevent Inside your script

在您的button click活动中 在您的脚本中

document.getElementById("idofradiobutton").checked = true;

JQuery solution:

jQuery 解决方案:

$("#idofradiobutton").prop("checked", true);

//....TAKE THE SELECTED VALUE OF RADIO BUTTON

//....取单选按钮的选定值

var radiovalue= document.getElementById('idofradiobutton').value;




if (radiovalue=="1") {
 document.getElementById('idofcorespondingradiobutton').checked = true;
}

//if you are using radiobutton list then try this

//如果您正在使用单选按钮列表,那么试试这个

function getCheckedRadio() {
  var radioButtons = document.getElementsByName("IDofRadiobuttonlist");
  for (var x = 0; x < radioButtons.length; x ++) {
    if (radioButtons[x].checked) {
      alert("You checked " + radioButtons[x].id + " which has the value " + radioButtons[x].value);
    }
  }
}

回答by Vishal Surjuse

Get RadioButtonList Selected value using jQuery

使用 jQuery 获取 RadioButtonList Selected 值

var selectedVal=$("[id$='RadioButtonList1']").find(":checked").val();

回答by Deepak_007

function Validation(){

var radioButtonlist=document.getElementById(<%=rdbdeductionList.ClientID%>");


    var inputs = radioButtonlist.getElementsByTagName('input');
    var flag = false;
    var selected;

    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].checked) {
                   selected = inputs[i];
                   flag = true;
                   break;
        }
    }


    if (flag) {

              alert(selected.value);
     }

    else {

         alert('Please select an option.!');
         return false;

        }
}

回答by prog1011

Try This

尝试这个

 var elementRef = document.getElementById("rdbRemarks"); // ID of DropDown
 for (var i=0; i<elementRef.options.length; i++)
 {
     if ( elementRef.options[i].value == "1") // value you want to select 
     {
        elementRef.options[i].selected = true;   
     }
 }

OR(JQuery)

( JQuery)

    var value = 1;
    $("input[id=rdbRemarks][value=" + value + "]").attr('checked', 'checked');
                                      //OR
    $("input[id=mygroup][value=" + value + "]").prop('checked', true);

回答by SharK

The selected value can be retrieved using JavaScript with the line below:

可以使用 JavaScript 使用以下行检索所选值:

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