Javascript 使用 jquery 清除单选按钮列表选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12482532/
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 10:29:09  来源:igfitidea点击:

clear radiobutton list selection using jquery

javascriptjqueryasp.net

提问by nav100

I would like to clear Radiobutton list selection after user enters the text in the TextBox. I tried the following code but it doesn't seem to be working and it doesn't show any error. Please let me know if there are any suggestions.

我想在用户在 TextBox 中输入文本后清除 Radiobutton 列表选择。我尝试了以下代码,但它似乎不起作用,也没有显示任何错误。如果有任何建议,请告诉我。

function ClearRadioButtonList() {
    var checkboxlistid9 = "#<%= rblLst.ClientID %>";
    $('.checkboxlistid9').attr('checked',false);     
}

<telerik:RadMaskedTextBox ID="txtCode" runat="server" Mask="###-##-####" SelectionOnFocus="CaretToBeginning">
    <ClientEvents  OnBlur="ClearRadioButtonList" />
</telerik:RadMaskedTextBox>

<asp:RadioButtonList ID="rblLst" runat="server" RepeatDirection="Horizontal">
    <asp:ListItem Value="1">Unknown</asp:ListItem>
    <asp:ListItem Value="2">Not Applicable</asp:ListItem>
</asp:RadioButtonList>

回答by Jupaol

Instead of:

代替:

$('.checkboxlistid9').attr('checked',false);

Try:

尝试:

$('.checkboxlistid9').removeAttr('checked');

Additionally I think your jQuery selector is wrong

另外我认为你的 jQuery 选择器是错误的

$('.checkboxlistid9')

I don't see a class checkboxlistid9on your asp:RadioButtonList

我没有看到checkboxlistid9你的课程asp:RadioButtonList

Change the query selector to:

将查询选择器更改为:

$("table[id$=rblLst] input:radio:checked").removeAttr("checked");

Or

或者

$("table[id$=rblLst] input:radio").each(function (i, x){
    if($(x).is(":checked")){
        $(x).removeAttr("checked");
    }
});

回答by Musa

The radio buttons will be descendants of the element that represents the RadioButtonList, you can select them with #<%= rblLst.ClientID %> input[type=radio]and use .prop()to remove the checked property.

单选按钮将是代表 的元素的后代RadioButtonList,您可以#<%= rblLst.ClientID %> input[type=radio]使用它们来选择它们并使用它.prop()来删除选中的属性。

function ClearRadioButtonList() {
    $("#<%= rblLst.ClientID %> input[type=radio]").prop('checked',false);     
}

回答by Adriano Carneiro

You should be using prop(). Also, each()to iterate:

你应该使用prop(). 另外,each()要迭代:

$('.checkboxlistid9').each(function (index, elem){
    $(elem).prop('checked',false);
})

回答by Aghilas Yakoub

$('.checkboxlistid9').removeAttr('checked');

回答by Pranesh Janarthanan

if it is i-checks based dynamic radio button list

如果是基于 i-checks 的动态单选按钮列表

<table id="Body_TTBody_rblAttendanceStatus" class="i-checks rblStatus">
<tbody>
<tr>
<td>
    <div class="iradio_flat-red" style="position: relative;">
        <input id="Body_TTBody_rblAttendanceStatus_0" type="radio">
    </div>
    <label for="Body_TTBody_rblAttendanceStatus_0" class="">Absent</label></td>
</tr>
<tr>
<td>
    <div class="iradio_flat-red" style="position: relative;">
        <input id="Body_TTBody_rblAttendanceStatus_1" type="radio">
    </div>
    <label for="Body_TTBody_rblAttendanceStatus_1" class="">Planned Leave</label> 
</td>
</tr>
<tr>
 <td>
    <div class="iradio_flat-red" style="position: relative;">
        <input id="Body_TTBody_rblAttendanceStatus_2" type="radio">
    </div>
    <label for="Body_TTBody_rblAttendanceStatus_2" class="">Present</label></td>
</tr>
<tr>
<td>
    <div class="iradio_flat-red" style="position: relative;">
        <input id="Body_TTBody_rblAttendanceStatus_3" type="radio">
    </div>
    <label for="Body_TTBody_rblAttendanceStatus_3" class="">Unplanned Leave</label> 
 </td>
 </tr>
</tbody>
</table>

 <script type="text/javascript">
 jQuery('#Body_TTBody_rblAttendanceStatus > tbody > tr> td').each(function (index, value) {
  if ($(value).find('input[type="radio"]').prop('checked')) {
     $(value).find('input[type="radio"]').prop('checked', false);
     $(value).find('input[type="radio"]').closest("div").removeClass("checked");
    }
});
</script>