Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未使用jquery和javascript选中复选框_Javascript_Jquery_Html - Fatal编程技术网

未使用jquery和javascript选中复选框

未使用jquery和javascript选中复选框,javascript,jquery,html,Javascript,Jquery,Html,我有一个arrayList,它是var totalCheckBoxArray=[1,2,3] 我有值为1、2和3的复选框: <div class="card-body"> <h5>Document List</h5> <div class="form-check"> <div class="form-group" id="documents"> <label> <input

我有一个arrayList,它是
var totalCheckBoxArray=[1,2,3]

我有值为1、2和3的复选框:

  <div class="card-body">
   <h5>Document List</h5>
   <div class="form-check">
      <div class="form-group" id="documents">
         <label> <input id="check_id1" type="checkbox" value="1" class="chk1" > <span>Invoice</span>
         <br>
         </label>
         <div style="padding-bottom: 5px"></div>
         <label> <input id="check_id2" type="checkbox" value="2" class="chk2" > <span>Packing List</span>
         <br>
         </label>
         <label> <input id="check_id3" type="checkbox" value="3" class="chk3" checked=""> <span>OTHERS</span>
         <br>
         </label>
      </div>
   </div>
</div>
我需要将
totalCheckBoxArray
的值与
trDataSecondTable.docletPrevious.docId的值进行比较,如果它们的值匹配,则需要选中复选框。例如:如果值为1的totalCheckBoxArray与任何trDataSecondTable.docletPrevious.docId=1匹配,则必须选中
Invoice
。所以我试着这样做,但没有被检查

 if(trDataSecondTable.docLetterPrevious){ 
  for (var i = 0; i <trDataSecondTable.docLetterPrevious.length ; i++) {
   for(j=0;j<totalCheckBoxArray.length;j++){

        if (totalCheckBoxArray[j] == trDataSecondTable.docLetterPrevious[i].docId) {
            console.log("entered in check");
            $(".chk"+(i+1)).prop('checked',true);
        } 
        else {
            $(".chk"+(i+1)).prop('checked', false);
             }

    }       
    }

     } 
if(trdatasecondable.docLetterPrevious){

对于(var i=0;i,with document.queryselector将获取所有输入并在数组中插入其各自的值。迭代数据并比较该数组中是否存在id。如果存在id,则将具有相应值的输入标记为true

var数据=[
{  
“信号”:13,
“docId”:1,
“单据名”:“发票”,
“docNameEng”:“发票”,
“docFile”:空,
“entryBy”:“user98”,
“进入日期”:“2019-02-05 13:03:02”,
“序号”:1,
“othersDescription”:空
},
{  
“信号”:13,
“docId”:2,
“文件名”:“装箱单”,
“docNameEng”:“装箱单”,
“docFile”:空,
“entryBy”:“user98”,
“进入日期”:“2019-02-05 13:03:02”,
“序号”:1,
“othersDescription”:空
},
{  
“信号”:13,
“docId”:3,
“文件名”:“其他”,
“docNameEng”:“其他”,
“docFile”:空,
“entryBy”:“user98”,
“进入日期”:“2019-02-05 13:03:02”,
“序号”:1,
“其他说明”:“添加”
},
{  
“信号”:13,
“docId”:3,
“文件名”:“其他”,
“docNameEng”:“其他”,
“docFile”:空,
“entryBy”:“user98”,
“进入日期”:“2019-02-05 13:03:02”,
“序号”:2,
“其他说明”:“adffff”
}
]
var val=document.querySelectorAll('input[type=checkbox]');
var-arr=[];
val.forEach((e)=>arr.push(数字(e.value)))
data.forEach((e)=>{
if(包括(如文件ID))
{
val.forEach((x)=>e.docId==x.value?x.checked=true:false)
}
})

文件清单
发票联

装箱单
其他
使用此代码

var trdatasecondable={docLetterPrevious:[]};
$('input:checkbox')。removeAttr('checked');
trDataSecondTable.docLetterPrevious=[
{  
“信号”:13,
“docId”:1,
“单据名”:“发票”,
“docNameEng”:“发票”,
“docFile”:空,
“entryBy”:“user98”,
“进入日期”:“2019-02-05 13:03:02”,
“序号”:1,
“othersDescription”:空
},
{  
“信号”:13,
“docId”:2,
“文件名”:“装箱单”,
“docNameEng”:“装箱单”,
“docFile”:空,
“entryBy”:“user98”,
“进入日期”:“2019-02-05 13:03:02”,
“序号”:1,
“othersDescription”:空
},
{  
“信号”:13,
“docId”:2,
“文件名”:“其他”,
“docNameEng”:“其他”,
“docFile”:空,
“entryBy”:“user98”,
“进入日期”:“2019-02-05 13:03:02”,
“序号”:1,
“其他说明”:“添加”
},
{  
“信号”:13,
“docId”:3,
“文件名”:“其他”,
“docNameEng”:“其他”,
“docFile”:空,
“entryBy”:“user98”,
“进入日期”:“2019-02-05 13:03:02”,
“序号”:2,
“其他说明”:“adffff”
}
]
var totalCheckBoxArray=[1,2,3];
如果(trdatasecondable.docLetterPrevious){

对于(var i=0;您是否在控制台内的检查中输入调试输出<代码>是控制台。日志(“检查中输入”);打印四次,谢谢您的帮助:)呵呵
 if(trDataSecondTable.docLetterPrevious){ 
  for (var i = 0; i <trDataSecondTable.docLetterPrevious.length ; i++) {
   for(j=0;j<totalCheckBoxArray.length;j++){

        if (totalCheckBoxArray[j] == trDataSecondTable.docLetterPrevious[i].docId) {
            console.log("entered in check");
            $(".chk"+(i+1)).prop('checked',true);
        } 
        else {
            $(".chk"+(i+1)).prop('checked', false);
             }

    }       
    }

     }