我现在有两个checkbox比如:<input type="checkbox" name="test" value="1" checked="checked"/>
<input type="checkbox" name="test" value="2"/>
value="1"的checkbox加载时默认选中,如果要实现我选中value=“2”的checkbox后,把第一个checkbox设为不选中,并且不能全都都为不选中状态,类似于radiobutton的规则,请给一个function,急,谢谢大虾。!身上真的没分了,忘见谅!!!jquerycheckbox

解决方案 »

  1.   


    $("input[type='checkbox'][name='test']").click(function () {
            $(this).attr("checked", "checked").siblings().removeAttr("checked");
        });
      

  2.   

    $("input[type='checkbox'][name='test']")获取checkbox对象,你也可以给分组的checkbox赋同一个class值,然后通过class去checkbox对象 $(".classname")
      

  3.   

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>无标题文档</title>
    <script type="text/javascript">
    window.onload=function(){
    var ins=document.getElementsByTagName("input");
    var arr=[];
    for(var i=0;i<ins.length;i++){
    if(ins[i].getAttribute('name')=='test'){
    arr.push(ins[i]);
    }
    }
    var l=arr.length;
    for(var i=0;i<l;i++){
    (function(m){
    arr[m].onclick=function(){
    if(this.checked){
    for(var j=0;j<l;j++){
    if(arr[j]!=this){
    arr[j].checked=false;
    }
    }
    }else{
    arr[(m+1)%l].checked=true; }
    }
    })(i)
    }
    }
    </script>
    </head><body>
    <input type="checkbox" name="test" value="1" checked="checked"/>
    <input type="checkbox" name="test" value="2"/>
    <input type="checkbox" name="test" value="3"/>
    </body>
    </html>
    这样试试