日期:2014-05-18  浏览次数:20979 次

求CheckBox 2选一的方法
求CheckBox 2选一的方法

------解决方案--------------------
LZ貌似可以用Radio Box...
------解决方案--------------------
用RadioButton比较好,否则就写2个事件:
C# code
    protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox2.Checked = !CheckBox1.Checked;
    }
    protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox1.Checked = !CheckBox2.Checked;
    }

------解决方案--------------------
用jq 控制 挺简单的

HTML code


<head id="Head1" runat="server">
  <title></title>
  <script src="jquery-1.7.1.min.js" type="text/javascript"></script>
</head>
<body>
  <form id="form1" runat="server">
 
  <asp:CheckBox ID="TextBox1" runat="server" />
  <asp:CheckBox ID="TextBox2" runat="server" />
 
  </form>
  <script type="text/javascript">
  $(document).ready(function () {
   
   $(":checkbox").click(function(){
     $(":checkbox").reomveAttr("checked");
     $(this).attr("checked","checked");
  })  
  
  })
  </script>
</body>
</html>

------解决方案--------------------
不想刷屏可以用下面的客户端代码:

假设有两个checkBox,分别代表性别:

function TestMale() {

var chkMale = document.getElementById("chkMale");
var chkFemale = document.getElementById("chkFemale");

chkFemale.checked = !chkMale.checked;
}

function TestFemale() {

var chkMale = document.getElementById("chkMale");
var chkFemale = document.getElementById("chkFemale");
chkMale.checked = !chkFemale.checked;
}