0

我试图从列表中只选择一个复选框。这是我正在尝试的代码,但它似乎不起作用。我可以使用 RadioButtonlist 但它不允许我取消选择单选按钮。请告诉我。

$(document).ready(function () {
var checkboxlistid = "#<%= chkLst.ClientID %>"; 
$(checkboxlistid + " input:checkbox").click(function () { 
 $(this).attr("checked",""); });

});

                    <asp:CheckBoxList ID="chkLst" runat="server" RepeatDirection="Horizontal">
                        <asp:ListItem Value="U">Unknown</asp:ListItem>
                        <asp:ListItem Value="R">Ref</asp:ListItem>
                    </asp:CheckBoxList>
4

2 回答 2

2

那应该不是问题..基本上,当您选择一项时,您需要清除所有选中的项目,然后检查当前项目..

试试这个代码..

$(function() {
    $('[id*=chkLst] input[type="checkbox"]').on('click' , function(){
        // Caching all the checkboxes into a variable
        var checkboxes =  $('[id*=chkLst] input[type="checkbox"]');
        // If one item is checked.. Uncheck all and
        // check current item..
        if($(this).is(':checked')){
            checkboxes.attr('checked', false);
            $(this).attr('checked', 'checked');        
        }
    });    
});​

这是一个工作示例.. http://jsfiddle.net/sushanth009/hBSTC/2/

于 2012-09-05T15:34:00.627 回答
2

复选框是此任务的错误 UI 元素。用户期望单选按钮和复选框的某些行为 - 不要弄乱它。最好添加一个 CLEAR 按钮,而不是尝试使复选框的行为类似于单选按钮。

于 2012-09-05T15:17:44.453 回答