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

Jquery实现checkBox单选
<body>
  <form id="form1" runat="server">
  <div>
   
  <asp:CheckBox ID="CheckBox1" runat="server" Text="a" />
  <br />
  <asp:CheckBox ID="CheckBox2" runat="server" Text="b" />
  <br />
  <asp:CheckBox ID="CheckBox3" runat="server" Text="c"/>
   
  </div>
  </form>
</body>
脚本是这样写的
$(document).ready(function () {
  var lastDom = null;
  $("input[type='checkbox']").bind("click", function () {
  if (lastDom) lastDom.attr("checked", false);
  lastDom = $(this);
  })
  })

功能是实现了
但是有点问题
就是选中然后去掉,然后在选中的话就不行了,必须得选其他的才能选中这个。。求解决!

------解决方案--------------------
JScript code

$(document).ready(function() {
    var lastDom = null;
    $("input[type='checkbox']").bind("click", function() {
    if (lastDom && lastDom.attr("id") != $(this).attr("id")) {
            lastDom.attr("checked", false);

        }
        lastDom = $(this);
    })
})