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

前端 未结 13 2089
一生所求
一生所求 2020-12-16 15:20

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

相关标签:
13条回答
  • 2020-12-16 15:54
    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;
    }
    
    0 讨论(0)
  • 2020-12-16 15:58

    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;
        }
    }
    
    0 讨论(0)
  • 2020-12-16 16:00

    Try this to get the selected value from the RadioButtonList.

    var selectedvalue = $('#<%= yourRadioButtonList.ClientID %> input:checked').val()
    
    0 讨论(0)
  • 2020-12-16 16:01

    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"
    
    0 讨论(0)
  • 2020-12-16 16:02

    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.

    0 讨论(0)
  • 2020-12-16 16:03

    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.

    0 讨论(0)
提交回复
热议问题