js asynchronously implements checkbox selection

Reference: https://blog.csdn.net/long19901216/article/details/51374064

Implement the front-end interface to obtain the background code to realize the checkbox part of the selection:

django background code:

from django.forms.models import model_to_dict
def GetList(request):
    user=request.user
    # permissions=user.permission_id.all()
    #permissions=model_to_dict(permissions)
    if request.method=='POST':
        reslist={
            'checkboxlist':'1,2',
            'bb':2
        }
        return JsonResponse(reslist)

HTML code:

                       <div ">
                          <p>
                                            界面2
                              <input type="checkbox" name="abc" value="1" />权限
                              <input type="checkbox" name="abc" value="2" />角色
                              <input type="checkbox" name="abc" value="3" />组


                          </p>
                        </div>
//js:
 <script src="/static/js/jquery.min.js"></script>
  <script type="application/javascript">
 $(document).ready(function () {
     $.ajax({
         type: "post",
         url:'/permission/get_checkbox_list/',
         data:{
             'id':1,
             csrfmiddlewaretoken:"{{ csrf_token }}"
         },
         async:true,
         dataType: "json",
         success: function(data){
             //这个地方搞了好久,用split()页面上报split is not a function错误
             //后面在国外的网站上建议 在字符串后面加 +'',然后问题就解决了 @zhangll
             var str = data.checkboxlist+'';
             alert(str);
             //异步获取选中记录中包含的lsid分类集合,遍历集合,如果集合中的值与checkbox的value值相同,则选中
             $(str.split(",")).each(function (i,dom){
                 alert(i);
                 alert(666);
                 alert(dom);
                 $(":checkbox[value='"+dom+"']").prop("checked",true);
             });
         }
     });

 });


//注:var str =  data.checkboxlist+'',这里我在做这块的时候页面一



    </script>

Show results:

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324689440&siteId=291194637