input中checked复选框进行单选

1、使用jQuery的prop属性,如果为DOM对象,可直接给DOM队象加$() ;

2、判断元素是否选中;

if ( $(elem).prop("checked") ) 
if ( $(elem).is(":checked") ) 

3、prop() 与 attr()  的区别?

(1)$("#chk1").prop("checked")  = true/false;

    $("#chk1").attr("checked") = 'checked';

(2)$("#chk1").prop("checked",true);

   $("#chk1").attr("checked", "checked");  

例:

1、html

<li>
    <input type="checkbox" name="carcek1" style="width: 20px; height: 20px; margin: 10px 12px;">
</li>
<li>
    <input type="checkbox" name="carcek1" style="width: 20px; height: 20px; margin: 10px 12px;">
</li>

2、js

var carcek1 = $("input[name='carcek1']");
carcek1.click(function() {
      for(var i=0; i<carcek1.length; i++) {
          $(carcek1[i]).prop('checked',false);
      }
      $(this).prop('checked',true)
})

3、效果

        

 

posted @ 2017-09-13 11:54  党欣彤  阅读(1001)  评论(0编辑  收藏  举报