Jquery traverses to get/set the value of the checkbox

When using the public components integrated by the company's js to obtain the checkbox value under the dynamic form list, only the last selected value can be obtained. In order to solve this problem, the checkbox attribute is analyzed, and the method of traversing the value or assignment is realized.
As follows, if multiple cities are selected, only the value of Xiangtan can be obtained.
insert image description here
Through the analysis of the form data object, the properties of the single check box checkbox object. The attribute of the checkbox is placed under the context, the selected check value is true, and the option value is stored in value.
insert image description here
insert image description here

The form is a List array, and the class attribute is added to the checkboxes of each group of forms.
insert image description here
The method of traversing to get the form check box value is as follows

function getRegionCode() {
   //获取资源所有地市编码
   for(var i=0;i<data.rights.malltRightsResourceRelVoList.length;i++) {
      var regionCodeId = "malltRightsResourceRelVoList"+ i;
      var checkedVal ="";
      //获取一组表单所有选中 复选框的值
      $('.'+regionCodeId).each(function(){

         if ($(this).context.checked == true){
            checkedVal+=$(this).val()+',';
         }
      });

      //为表单数据赋值
      data.rights.malltRightsResourceRelVoList[i].regionCode =  checkedVal.substr(0,checkedVal.length-1);
      console.info("new:"+data.rights.malltRightsResourceRelVoList[i].regionCode);

      //
      if(!isValid(data.rights.malltRightsResourceRelVoList[i].regionCode)){
         tabResult1.push({msg:"请选择资源地市配置"});
      }
   }
}

Initialize access to display assignment

//获取资源所有地市编码
function regionInitCheck(){
   for(var i=0;i<data.rights.malltRightsResourceRelVoList.length;i++) {
      var regionCodeId = "malltRightsResourceRelVoList"+ i;
      var regionCodeList = data.rights.malltRightsResourceRelVoList[i].regionCode;
      console.info("初始化regionCodeList="+regionCodeList)
      $('.'+regionCodeId).each(function(){
         //console.info($(this))

         if (regionCodeList.indexOf($(this).context.value) != -1){
            $(this).context.checked = true;
         }
      });
   }
}

Guess you like

Origin blog.csdn.net/qq_44624722/article/details/117952853