jquery 获取checkbox选中的值

function fsubmit(){
var ids = '';
$(":checkbox[name='checktree']:checked").each(function(){
// alert($(this).val());
ids = ids +" " + $(this).val();
});
alert(ids );

alert(${roleId});
}

 

   1.遍历checkbox用each()方法:
       $("input[name='box']").each(function(){});
   2.设置checkbox被选中用attr();方法:
     $("input[name='box']").attr("checked","checked");
 

    在HTML中,如果一个复选框被选中,对应的标记为 checked="checked"。 但如果用jquery alert($("#id").attr("checked")) 则会提示您是"true"而不是"checked",所以判断 if("checked"==$("#id").attr("checked")) 是错误的,应该是 if(true == $("#id").attr("checked"))

 
  3.获取被选中的checkbox的值:
    $("input[name='box'][checked]").each(function(){
    if (true == $(this).attr("checked")) {
          alert( $(this).attr('value') );
    }
     或者:
    $("input[name='box']:checked").each(function(){
    if (true == $(this).attr("checked")) {
          alert( $(this).attr('value') );
    }
   $("input[name='box']:checked")与 $("input[name='box']")有何区别没试过,我试了用 $("input[name='box']")能成功。

   4.获取未选中的checkbox的值:

 

    $("input[name='box']").each(function(){
          if ($(this).attr('checked') ==false) {
                alert($(this).val());
            }
     });

   5.设置checkbox的value属性的值:

          $(this).attr("value",值);

 

posted on 2014-04-04 15:19  Ganymede505  阅读(2616)  评论(0编辑  收藏  举报