简体   繁体   中英

RadioButtonList selected value in javascript

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

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

In your button click event Inside your script

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

JQuery solution:

$("#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);
    }
  }
}

使用jQuery获取RadioButtonList选定的值

var selectedVal=$("[id$='RadioButtonList1']").find(":checked").val();
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;

        }
}

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 )

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

可以使用以下行的JavaScript来检索选定的值:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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