简体   繁体   中英

Asp.net Select RadioButtonList using JQUERY

Hi I have a radio button list

<asp:RadioButtonList ID="lista_ragionevolezza" runat="server" 
 DataSourceID="EntityDataSource_ragionevolezza" 
 DataTextField="descrizione" DataValueField="id" 
 SelectedValue='<%# Bind("id_ragionevolezza") %>' 
 class="radioButtonScegli" />

and I want to select this in a Javascript function using JQuery for clearing a selection

var rbList=$("." + "radioButtonScegli");

using this class selector, but the code didn't work. What is the correct way to select this object? Thanks

The class is added to a container in which .Net is placing those inputs. Something like this should work: $(".radioButtonScegli").find(':radio');

For clearing selection:

$('.radioButtonScegli').find(':radio').prop('checked',false);'

By ID

var radiolist= $('input:radio[id$="lista_ragionevolezza"]');

By class

var radiolist= $('input:radio[class="radioButtonScegli"]');

or just $('.radioButtonScegli')

to make checked/unchecked you can use either attr or prop

$('.radioButtonScegli').find(':radio').attr('checked',false);

$('.radioButtonScegli').find(':radio').prop('checked',false);

I suggest to not use the class selector, but the Client Id, because your javascript variable would change if you have more the 1 object which has the class radioButtonScegli .

Try selecting it this way:

var rbList = $('#<%=lista_ragionevolezza.ClientID%>')

This way rbList will only contain this one RadiobuttonList. And since this is an element containing 1 to n checkboxes, you need to iterate over each item to uncheck it:

rbList.each(function (i, chkbox){
    if($(chkbox).is(":checked")) {
        $(chkbox).removeAttr("checked");
    }
});

您必须使用对象的ID:

var rblist = $("#lista_regionevolezza");

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