2012-07-27 139 views

回答

4

尝试此

前端

function radioMe(e) { 
    if (!e) e = window.event; 
    var sender = e.target || e.srcElement; 

    if (sender.nodeName != 'INPUT') return; 
    var checker = sender; 
    var chkBox = document.getElementById('<%= chks.ClientID %>'); 
    var chks = chkBox.getElementsByTagName('INPUT'); 
    for (i = 0; i < chks.length; i++) { 
     if (chks[i] != checker) 
     chks[i].checked = false; 
    } 
} 

<asp:CheckBoxList runat="server" ID="chks"> 
    <asp:ListItem>Item</asp:ListItem> 
    <asp:ListItem>Item</asp:ListItem> 
    <asp:ListItem>Item</asp:ListItem> 
    <asp:ListItem>Item</asp:ListItem> 
</asp:CheckBoxList> 

后端

protected void Page_Load(object sender, EventArgs e) 
{ 
     chks.Attributes.Add("onclick", "radioMe(event);"); 
} 

但是在做这一切,你应该考虑RadioButtonList控制

+0

这不工作.. – 2012-07-27 12:34:46

+0

请原谅我在IE中没有在FF中工作,让我检查一下 – yogi 2012-07-27 12:35:58

+0

@romfernando现在试一试,我已经编辑它 – yogi 2012-07-27 12:52:06

5

使用在RadioButtonList的一个的CheckBoxList可有时方便,因为它允许用户取消选择的选择,而不必给我发明一个解决方法。遵循瑜珈的回答,我想要一种方法,允许轻松重用,而不必将CheckBoxList的ClientID插入到每个用途的函数中。感谢模板瑜伽师。

  • 用法:

    <asp:CheckBoxList ID="m_testControl" runat="server" data-toggle="radio" RepeatDirection="Horizontal"> 
        <asp:ListItem Value="CON">Concur</asp:ListItem> 
        <asp:ListItem Value="CWI">Concur With Intent</asp:ListItem> 
        <asp:ListItem Value="DNC">Do Not Concur</asp:ListItem> 
    </asp:CheckBoxList> 
    
  • JQuery的功能:

    $(document).ready(function() 
    { 
        // turn all CheckBoxLists labeled for 'radio' to be single-select 
        $('[data-toggle=radio]').each(function() 
        { 
         var clientId = $(this).attr('id'); 
         $(this).find('input').each(function() 
         { 
          // set parent's id 
          $(this).attr('data-parent', clientId); 
    
          // add an onclick to each input 
          $(this).click(function (e) 
          { 
           // ensure proper event 
           if (!e) e = window.event; 
           var sender = e.target || e.srcElement; 
           if (sender.nodeName != 'INPUT') return; 
    
    
           // toggle off siblings 
           var id = $(this).attr('id'); 
           var parent = $(this).attr('data-parent'); 
           $('input[data-parent=' + parent + ']').not('#' + id).prop('checked', false); 
          }); 
         }); 
        }); 
    }); 
    
0
$('input[type=checkbox]').click(function() { 
      var chks = document.getElementById('<%= chkBranchRoleTransaction.ClientID %>').getElementsByTagName('INPUT'); 
      for (i = 0; i < chks.length; i++) { 
        chks[i].checked = false; 
      } 
      $(this)[0].checked = true; 
     }); 
     // or// 
     $('input[type=checkbox]').click(function() { 
      var chkBox = document.getElementById('<%= chkBranchRoleTransaction.ClientID %>'); 
      var chks = chkBox.getElementsByTagName('INPUT'); 
      for (i = 0; i < chks.length; i++) { 
       chks[i].checked = false; 
      } 
      $(this)[0].checked = true; 
     }); 
    });[enter image description here][1] 


    [1]: http://i.stack.imgur.com/zzaaz.jpg 
0
var id = ""; 
$("#CblProduct").on('click', ':checkbox', function() {  
    if (id != "") 
    { 
     $(id).attr('checked', false); 
     $(this).attr('checked', true); 
    } 
    if($("#CblProduct").length > 0) 
    { 
     id = this; 
    } 
}); 
0
static int indexOfL=0;// the index of initial selected item 
    protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     int i = 0; 

     foreach (ListItem li in CheckBoxList1.Items) 
     {  
      { 
       if (i != indexOfL && li.Selected) 
       {  
        indexOfL=i; 
        CheckBoxList1.ClearSelection(); 
        li.Selected = true; 

       } 
       i++; 
      } 
     }} 

我保存所选项目的索引,当有变化时,我们得到两个选定的项目,所以在indexOfL的帮助下,我们得到了正确的项目。 我不“知道,如果这种方式是最好的,因为这个代码在服务器端运行.... 希望它可以帮助...

相关问题