Do editing, jquery and PHP, from the background according to the value of ID read, edit the front checkbox and select, how according to the value from the background, let the corresponding checkbox and option for the selected state,

  question

Do editing, jquery and PHP, from the background according to the value of ID read, edit the front checkbox and select, how according to the value from the background, let the corresponding checkbox and option for the selected state,

<!  DOCTYPE html>
 <html lang="zh-CN">
 <head>
 <meta charset="UTF-8">
 < title > test < /title >
 <?  Php
 $chk = array('ch1','ch3','ch5');  //The background obtains the array of id selected by checkbox.
 $slt = 'iph';  //Background Get value of option selected by Select
 ?  >
 <script type="text/javascript" src="//upcdn.b0.upaiyun.com/libs/jquery/jquery-1.8.3.min.js"></script>
 
 
 <script type="text/javascript">
 $(function(){
 
 var chkData = <?  php echo json_encode($chk);  ?  >;
 var sltData = '<?  php echo $slt;  ?  >';
 
 $('.chk').each(function(){
 var self = $(this);
 var selfId = self.attr('id');
 $.each(chkData, function(n, v){
 if(v == selfId) self.attr('checked' , 'true');
 });
 });
 
 $('#select option').each(function(){
 var self = $(this);
 var selfVal = self.val();
 if(selfVal == sltData) self.attr('selected' , 'true');
 });
 
 });
 </script>
 
 
 </head>
 <body>
 
 <p><input id="ch1" class="chk" type="checkbox" value="111">111</p>
 <p><input id="ch2" class="chk" type="checkbox" value="222">222</p>
 <p><input id="ch3" class="chk" type="checkbox" value="333">333</p>
 <p><input id="ch4" class="chk" type="checkbox" value="444">444</p>
 <p><input id="ch5" class="chk" type="checkbox" value="555">555</p>
 
 <div class="selectAera">
 <select id="select">
 < option value="smt "> hammer < /option >
 < option value="iph "> apple < /option >
 < option value="xm "> millet < /option >
 < option value="nka "> Nokia < /option >
 </select>
 </div>
 
 </body>
 </html>

Running results:

请输入图片描述