大家在写视图层这样写
<?= Html::beginForm(['order/update'], 'post', ['enctype' => 'multipart/form-data']) ?> <table > <tr> <td>商品分类</td> <td> <select name="" class="type" > <?php foreach ($type as $key => $v) { ?> <option value="<?php echo $v['id'] ?>"><?php echo $v['goodstype'] ?></option> <?php } ?> </select> </td> </tr> <?php foreach ($goods as $key => $val) { ?> <tr> <td><input type="checkbox" name="" id="<?=$val['id']?>" value="<?php echo $val['id'] ?>"></td> <td><?php echo $val['goods'] ?></td> </tr> <?php } ?> </table> <input type="button" id="save" value="保存"><?= Html::endForm() ?>
------------------------------
下面是重点jq写法通过属性来修改下拉框的属性
$(".type").change( function() { $("input:checked").attr("checked",false); var id=$(this).val(); var url="<?php echo url::to(['goods/upd']) ?>" $.Ajax({ type:"post", url:url, data:{id:id}, dataType:"json", success:function(msg){ $.each(msg,function(y,m) { // alert( "#"+m.id); $("#"+m.id).PRop('checked','true'); }); } }) });
对应后台处理过程
public function actionUpd() { $request = Yii::$app->request; $id = $request->post('id'); // var_dump($id); $connection = /Yii::$app->db; $sql="SELECT id FROM goods where tid='$id'"; $command = $connection->createCommand($sql); $posts = $command->queryAll(); // var_dump($posts); echo json_encode($posts); }
2重点修改/**修改本分类下的商品并保存 */$(document).on('click','#save',function(){ var tid=$('.type').val(); // alert(tid); var ch=$("input:checked"); var arr=[]; $.each(ch,function(y,m) { if(ch.eq(y).prop("checked")){ arr.push(ch.eq(y).val()); }; }); ids=arr.join(",",arr); // alert(ids); $.ajax({ type: "POST", url: "<?= url::to(['goods/up'])?>", data:{tid:tid,ids:ids}, success: function(msg){ alert(msg); } }); })
我们再看看他对应后台处理
public function actionUp() { $request = Yii::$app->request; $tid = $request->post('tid'); // var_dump($tid); $ids=$request->post('ids'); // var_dump($ids); $connection = /Yii::$app->db; $sql="update goods set tid='$tid' where id in($ids)"; $command = $connection->createCommand($sql); $res=$command->execute(); var_dump($res); } /**
新闻热点
疑难解答