In doing checkbox controls with ajax, abnormal values

Attr value can not be used, otherwise there is a problem refresh, abnormal values, the values ​​must prop.

$.ajax({
        url:"${pageContext.request.contextPath}/getAdmin/"+$(this).attr("edit_id"),
        type:"GET",
        success:function(result){
            $("#name").text(result.name);
            $("#email").val(result.email);
            $("#role.id").val(result.role.id);
            
            $("#lockFlag").prop("checked",result.lockFlag); //关键代码
            console.log($("#lockFlag").prop('checked')) ;
            $("#btn_modify").attr("edit_id",result.id);
            
        }
    })

 

参考文献,如下:

A checkbox for each name attribute value, then the determination value is not true, the state changes to true. Add prop manner use herein, can not be used attr (having a property of true and false two attributes, such as checked, selected or disabled using prop (), others use attr (), and then acquires jQuery1.9 versions used attr checkbox the value is undefined, the result is checked in version 1.8.3 and undefined).

I am in this approach is to first table form out of the cycle, and then change the state, use the following code

if(da.P1[i].aa==true){
    $(":checkbox[name='check"+i+"']").prop("checked",true);
 
};
    if(da.P1[i].bb==true){
    $(":checkbox[name='checks"+i+"']").prop("checked",true);

};

 

Guess you like

Origin www.cnblogs.com/sdgtxuyong/p/12090213.html