Checkbox选中input标签取值和回填input标签

html:

<td>

<input name="ck" type="checkbox" value="1"/><span>二哈span>

<input name="ck" type="checkbox" value="2"/><span>泰迪span>

<input name="ck" type="checkbox" value="3"/><span>柯基span>

td>

引用jQuery.min.js

Js

CheckBox为单选:

$("input:[type= radio]:checked").val();

或者

$("input: radio:checked").val()
或者

$("input:[name='ck']:checked").val();

 

输出如下图所示

Checkbox选中input标签取值和回填input标签_第1张图片

 

CheckBox为多选:

        $("input:checkbox").each(function () {

            $(this).click(function () {

            //console.log($(this))

                if ($(this)[0].checked == true) {

                        console.log($(this).val());

                  

                    }

                })

           })

 

输出如下图所示

Checkbox选中input标签取值和回填input标签_第2张图片

获取全部值

$('input:checkbox').each(function () {

console.log($(this)[0].defaultValue)

});

 

结果如下图

Checkbox选中input标签取值和回填input标签_第3张图片

全选:

$('input:checkbox').each(function() {
        $(this).attr('checked', true);
});

 

全不选:

$('input:checkbox').each(function () {
        $(this).attr('checked',false);
});

 

CheckBox回填:

$('input:checkbox:first').attr("checked",'checked');
或者
$('input:checkbox').eq(‘+索引变量+’).attr("checked",'true');
或者
$('input:checkbox[value='+CheckBox值+']').attr('checked','true');
多个回填:

$('input:radio').slice(0,2).attr('checked','true');

 

CheckBox只能单选

$(":checkbox").click(function(){ 
  if($(this).is(':checked')){
    $(this).attr('checked',true).siblings().attr('checked',false);
  }
});

你可能感兴趣的:(html,js)