我现在想用jquery来设置chekbox的值,就是我点击那个框设置他的值为1,在点下(即对号取消)让他的值为0,我写的如下,但有问题,请问如何改,或写!
<script>        $("#document").ready(function(){
$("#sfqy").click(function(){

               if($(this).attr("checked")){
                       $(this).removeAttr("checked");
                       $(this).val("0");
                     alert($(this).val());  
                   }
               else{
              
                $(this).attr("checked",'true');
                  $(this).val("1");
                  alert($(this).val());
                   } }) })
</script>下面是我的checkbox及属性
<td>
<label><input type="checkbox" id="sfqy" name="news.SFQY" value="嘿嘿">是否启用</label>
</td>

解决方案 »

  1.   

    if($("#ID").attr("checked")){
       $("#employment_present").attr("checked",true);
    } else{$("#id").attr("checked",false);}
      

  2.   

    $("#sfqy").click(function (){            var c=$("#sfqy").attr("checked");
                   if(c){
                       $("#sfqy").val("1");
                       alert($("#sfqy").val());
                
                       }
                   else{
                       $("#sfqy").val("0");
                       alert($("#sfqy").val());
                       }
    })
    这么做就对了
      

  3.   

    呃,我还是第一次见到这种写法:$("#document").ready(function()...不解啊先说说lz的一个缺陷,不该在checkbox的click事件里判断...也许应该用个按钮什么的.最后贴出我的:   if($("#sfqy").attr("checked")==true) //判断checkbox是否被选中