Jquery 中 $('obj').attr('checked',true)失效的幾種解決方案

$('input[name="warehouseLevel"]').each(function(i,obj){

$(this).attr("checked",false);

if(row.isQuery==$(this).val()){

$(this).attr("checked",true);

}

});

開發中發現這樣radio只能選中一次 後面就無法選中,


最後找到解決方法:

1、$('obj').prop('checked',true)

2、

$(input[name="warehouseLevel"].each(function(){

    this.checked=true;

    })

爲什麼:attr爲失效?因爲checked屬於爲原型對象的屬性。而attr在remove原型對象時會出錯。原型對象指的是自身自帶的,無法移除。prop會忽略這個錯誤。而attr操作的是普通非原型對象(可移除)。js 的dom對象屬性是可以隨意增加的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章