美文网首页
>>>>> jQuery中判断和赋值checked的几种方法

>>>>> jQuery中判断和赋值checked的几种方法

作者: 風隨風去 | 来源:发表于2016-12-14 15:24 被阅读0次

判断checked的方法

  • .attr()
 $('input').attr('checked');//jQ版本为1.6+返回'checked'或'undefined';版本为1.5-返回true/false
  • .prop()
$('input').prop('checked');//jQ版本为1.6+返回true/false
  • .is()
$('input').is(':checked');//jQ所有版本都返回true/false---*注:记住加冒号!!!

checked的赋值

  • .attr()---适合jq所有的版本
$('input').attr('checked','checked');
$('input').attr('checked',true/false);
  • .prop()---版本1.6+可用
$('input').prop('checked',true);
$('input').prop('checked','checked');
$('input').prop({checked:true}); //键值对--true/false
$('input').prop('checked',function(){
       return true;//返回true/false
});
  • 通过获取原生DOM元素设置checked并赋值
$('input').each(function(i,element){
    element.checked=true; //true/false
})

相关文章

网友评论

      本文标题:>>>>> jQuery中判断和赋值checked的几种方法

      本文链接:https://www.haomeiwen.com/subject/eevimttx.html