js实现全选,全不选,反选,批量删除等(Yii)

效果展示:


代码实现:

  1. <?php  
  2. header("content-type:text/html;charset=utf-8");  
  3. use yii\helpers\Html;  
  4. use yii\widgets\LinkPager;  
  5. //print_r($countries);die;  
  6. ?>  
  7. <h1>显示出数据</h1>  
  8. <input type="checkbox" value="全选" οnclick="check(this);">全选  
  9. <input type="checkbox" value="全不选" οnclick="check_bx(this);">全不选  
  10. <input type="checkbox" value="反选" οnclick="check_fx();">反选  
  11. <input type="checkbox" value="批量删除" οnclick="check_del();">批量删除  
  12.   
  13. <script src="public/jq.js"></script>  
  14. <style media="screen">  
  15.   tr{  
  16.     background-color: red;  
  17.     font-family: 宋体;  
  18.     width: 100px;  
  19.     height: 30px;  
  20.     line-height: 30px;  
  21.     text-align: center;  
  22.   }  
  23.   td{  
  24.     background-color: pink;  
  25.     border: 2px solid purple;  
  26.   }  
  27.   .aa{  
  28.     border: 2px solid green;  
  29.     background-color: yellow;  
  30.     font-family: 隶书;  
  31.   }  
  32.   h1{  
  33.     font-family: 华文行楷;  
  34.     box-shadow: 10px 10px 5px #888888;    
  35.     border:2px solid;    
  36.     border-radius:25px;   
  37.     width: 200px;   
  38.     background-color: white;  
  39.   }  
  40.   th{  
  41.     font-family: 隶书;  
  42.     border: 2px solid green;  
  43.     background-color: yellow;  
  44.   }  
  45.   .checkbox{  
  46.     width: 25px;  
  47.     height: 30px;  
  48.   }  
  49. </style>  
  50. <table border="1">  
  51.     <th></th>  
  52.     <th>序列号</th>  
  53.     <th>父级ID</th>  
  54.     <th>地区名称</th>  
  55.     <th>操作</th>  
  56.     <?php foreach ($countries as $k => $v) {  ?>  
  57.         <tr>  
  58.           <td><input type="checkbox" id="<?php echo $v['r_id'] ?>" class="checkbox" name="check[]" value="<?php echo $v['r_id'] ?>"></td>  
  59.           <td><?php echo $v['r_id'] ?></td>  
  60.           <td><?php echo $v['pid'] ?></td>  
  61.           <td><span class="num" id="<?php echo $v['r_id'] ?>"><?php echo $v['r_name']?></span></td>  
  62.           <td><a href="javascript:void(0)" id="<?php echo $v['r_id'] ?>" class="aa" οnclick="del(this)">删除 </a></td>  
  63.         </tr>  
  64.    <?php } ?>  
  65. </table>  
  66. <?= LinkPager::widget(['pagination' => $pagination]) ?>  
  67. <script type="text/javascript">  
  68.   //即点即改入库  
  69.   $(function(){  
  70.     $(document).on('click','.num',function(){  
  71.         var id=$(this).attr('id');  
  72.         var _this=$(this);  
  73.         var new_val=$(this).html();  
  74.         _this.parent().html("<input type='text'class='asdf' value="+new_val+" id="+id+">");  
  75.         var inp=$('.asdf');  
  76.         inp.focus();  
  77.         inp.blur(function(){  
  78.             var old_id=$(this).attr('id');  
  79.             var old_val=$(this).val();  
  80.             //inp.parent().html("<span class=\"num\" id="+old_id+">"+old_val+"</span>");  
  81.             $.get("index.php?r=upload/updates",{measure_unit:old_val,id:old_id},function(e){  
  82.                 if(e==1){  
  83.                     inp.parent().html("<span class=\"num\" id="+old_id+">"+old_val+"</span>");  
  84.                 }else{  
  85.                     inp.parent().html("<span class=\"num\" id="+old_id+">"+new_val+"</span>");  
  86.                 }  
  87.             })  
  88.         })  
  89.     })  
  90.   })  
  91. </script>  
  92. <script>  
  93.     //无刷新删除  
  94.       function del(obj)  
  95.     {  
  96.         var ids=obj.id;  
  97.         ajax=new XMLHttpRequest();  
  98.         ajax.onreadystatechange=function()  
  99.         {  
  100.             if(ajax.readyState==4)  
  101.             {  
  102.                 //alert(ajax.responseText);  
  103.                 if(ajax.responseText==1)  
  104.                 {  
  105.                     obj.parentNode.parentNode.parentNode.removeChild(obj.parentNode.parentNode);  
  106.                 }  
  107.                 else{  
  108.                     alert("删除失败");  
  109.                 }  
  110.             }  
  111.         }  
  112.         ajax.open("get","index.php?r=upload/del&ids="+ids);  
  113.         ajax.send(null);  
  114.     }  
  115.     /*全选*/  
  116.         function check(obj)  
  117.     {  
  118.         var ids=document.getElementsByName("check[]");  
  119.         if(obj.checked)  
  120.         {  
  121.             for(var i=0;i<ids.length;i++)  
  122.             {  
  123.                 ids[i].checked=true;  
  124.             }  
  125.         }  
  126.     }  
  127.     /*全不选*/  
  128.      function check_bx(obj)  
  129.     {  
  130.         var ids=document.getElementsByName("check[]");  
  131.         if(obj.checked)  
  132.         {  
  133.             for(var i=0;i<ids.length;i++)  
  134.             {  
  135.                 ids[i].checked=false;  
  136.             }  
  137.         }  
  138.     }  
  139.       //反选  
  140.     function check_fx()  
  141.     {  
  142.         var ids=document.getElementsByName("check[]");  
  143.         for(var i=0;i<ids.length;i++)  
  144.         {  
  145.             ids[i].checked=!ids[i].checked;  
  146.         }  
  147.     }  
  148.     /*批量删除*/  
  149.      function check_del()  
  150.     {  
  151.         var ids=document.getElementsByName("check[]");  
  152.         var str='';  
  153.         for(var i=0;i<ids.length;i++)  
  154.         {  
  155.             if(ids[i].checked)  
  156.             {  
  157.                 str=str+','+ids[i].value;  
  158.             }  
  159.         }  
  160.         new_str=str.substr(1);  
  161.         ajax=new XMLHttpRequest();  
  162.         ajax.onreadystatechange=function() {  
  163.             if (ajax.readyState == 4) {  
  164.                 //alert(ajax.responseText);    
  165.                 if(ajax.responseText==1)  
  166.                 {  
  167.                     for(var j=ids.length-1;j>=0;j--)  
  168.                     {  
  169.                         if(ids[j].checked)  
  170.                         {  
  171.                           ids[j].parentNode.parentNode.parentNode.removeChild(ids[j].parentNode.parentNode);  
  172.                         }  
  173.                     }  
  174.   
  175.                 }  
  176.                 else  
  177.                 {  
  178.                     alert("删除失败");  
  179.                 }  
  180.             }  
  181.         }  
  182.         ajax.open("get","index.php?r=upload/del_all&new_str="+new_str);  
  183.         ajax.send(null);  
  184.     }  
  185. </script>  

2.
  1. <?php  
  2. namespace app\controllers;  
  3.   
  4. use Yii;  
  5. use yii\filters\AccessControl;  
  6. use yii\web\Controller;  
  7. use yii\filters\VerbFilter;  
  8. use app\models\LoginForm;  
  9. use app\models\ContactForm;  
  10. use yii\web\UploadedFile;  
  11. use app\models\Upload;  
  12. use yii\data\Pagination;  
  13. use app\models\Country;  
  14. use app\models\Region;  
  15.   
  16. class UploadController extends Controller  
  17. {  
  18.     //public $layout=false; //禁用yii自带样式  
  19.     public function actionIndex(){  
  20.         $model = new Upload();  
  21.         if ($model->load(Yii::$app->request->post()) && $model->validate()) {  
  22.             // 验证 $model 收到的数据  
  23.             // 做些有意义的事 ...  
  24.             return $this->render('entry-confirm', ['model' => $model]);  
  25.         } else {  
  26.             // 无论是初始化显示还是数据验证错误  
  27.             return $this->render('entry', ['model' => $model]);  
  28.         }  
  29.     }  
  30.     public function actionAdds()  
  31.     {  
  32.         $model = new Upload();  
  33.         $request = Yii::$app->request;  
  34.         $post=$request->post('Upload');  
  35.         $u_name = $post['u_name'];  
  36.         $u_pwd = $post['u_pwd'];  
  37.         //在浏览器输出的值是 yii\web\UploadedFile Object ( [name] => 2.jpg [tempName] => C:\Windows\php3986.tmp  
  38.         // [type] => image/jpeg [size] => 216848 [error] => 0 )  
  39.          $arr =  $model->u_img = UploadedFile::getInstance($model,'u_img');  
  40.          //print_r($arr);  
  41.           if ($model->upload()){  
  42.                 $u_img = $arr->name;  
  43.                 //var_dump($u_img);  
  44.                  $connection = \Yii::$app->db;  
  45.                     $result=$connection->createCommand()->insert('upload', [  
  46.                 'u_name' => $u_name,  
  47.                 'u_pwd' => $u_pwd,  
  48.                  'u_img' =>$u_img,  
  49.             ])->execute();  
  50.             if($result)  
  51.             {  
  52.               echo "添加成功";  
  53.             }  
  54.             else  
  55.             {  
  56.                 echo "添加失败";  
  57.             }  
  58.             }  
  59.   
  60.     }  
  61.     /*分页*/  
  62.     public function actionLists()  
  63.      {  
  64.          $query = Country::find();  
  65.   
  66.          $pagination = new Pagination([  
  67.              'defaultPageSize' => 1,  
  68.              'totalCount' => $query->count(),  
  69.          ]);  
  70.   
  71.          $countries = $query->orderBy('name')  
  72.              ->offset($pagination->offset)  
  73.              ->limit($pagination->limit)  
  74.              ->all();  
  75.   
  76.          return $this->render('lists', [  
  77.              'countries' => $countries,  
  78.              'pagination' => $pagination,  
  79.          ]);  
  80.      }  
  81.          /*地区表进行分页*/  
  82.          public function actionShow(){  
  83.              $query = Region::find();  
  84.              $pagination = new Pagination([  
  85.                      'defaultPageSize' => 6,  
  86.                      'totalCount' => $query->count(),  
  87.              ]);  
  88.              $countries = $query->orderBy('r_id')  
  89.                      ->offset($pagination->offset)  
  90.                      ->limit($pagination->limit)  
  91.                      ->all();  
  92.              return $this->render('show', [  
  93.                      'countries' => $countries,  
  94.                      'pagination' => $pagination,  
  95.              ]);  
  96.          }  
  97.          /*修改*/  
  98.          public function actionUpdates(){  
  99.              $name = $_GET['measure_unit'];  
  100.             $id = $_GET['id'];  
  101.              $connection = \Yii::$app->db;  
  102.              $command = $connection->createCommand("UPDATE region SET r_name='$name' WHERE r_id='$id'");  
  103.              $command->execute();  
  104.              if(!empty($command)){  
  105.                  echo 1;  
  106.              }else{  
  107.                  echo 0;  
  108.              }  
  109.          }  
  110.          // 无刷新delete  
  111.          public function actionDel(){  
  112.             $id=$_GET['ids'];  
  113.             $connection=\Yii::$app->db;  
  114.             $arr=$connection->createCommand("delete from region where r_id='$id'")->execute();  
  115.             if($arr){  
  116.                 echo 1;  
  117.             }else{  
  118.                 echo 0;  
  119.             }  
  120.          }  
  121.          /*  
  122.         批量删除  
  123.         @new_str  GET  
  124.         */  
  125.          public function actionDel_all(){  
  126.             $new_str=$_GET['new_str'];  
  127.             $connection=\Yii::$app->db;  
  128.             $arr=$connection->createCommand("delete from region where r_id in($new_str)")->execute();  
  129.             if($arr){  
  130.                 echo 1;  
  131.             }else{  
  132.                 echo 0;  
  133.             }  
  134.          }  
  135. }  
  136. ?>  

另外一种方法:

  1. <h1>您得到的验证码为:<font id="sum" color="red"></font></h1>  
  2. <h2><font color="aqua">验证码的个数是:</font><input type="text" id="num"></h2>  
  3. <h3><input type="button" οnclick="fun();" value="点击生成"><input type="reset" value="刷新"></h3>  
  4. <html>  
  5. <title>点名表</title>  
  6. <fieldset style="color: blue">  
  7.     <legend><h2>1408phpE班级点名表</h2></legend>  
  8.     <h1>点名表</h1>  
  9.     <h3><textarea id="student"></textarea></h3>  
  10.     <input type="button" οnclick="fun1();" value="点击" id="one">  
  11. </fieldset>  
  12. </html>  
  13. <form>  
  14.     <p>  
  15.         <input type="checkbox" id="ab" οnclick="fun5();"> 全选/全不选<br>  
  16.         <input type="checkbox" value="反选" οnclick="fun6();" id="fa">反选<br>  
  17.         <input type="checkbox" name="a" value="吃饭">吃饭<br>  
  18.         <input type="checkbox" name="a" value="睡觉">睡觉<br>  
  19.         <input type="checkbox" name="a" value="打豆豆">打豆豆<br>  
  20.         <input type="checkbox" name="a" value="玩游戏">玩游戏<br>  
  21.         <input type="checkbox" name="a" value="看电影">看电影<br>  
  22.     </p>  
  23. </form>  
  24.   
  25.     <script>  
  26.         //点名表  
  27.         document.bgColor='green';  
  28.         function fun2(){  
  29.             var arr=Array('134','1234','14324','12342134','1234123424','34534','6434','342','234','234','342','2341','24','a','s','d','f','g','h','j','n','v','x','xw');  
  30.             var num=parseInt(Math.random()*arr.length);  
  31.             document.getElementById('student').innerHTML=arr[num];  
  32.         }  
  33.         function fun1(){  
  34.             if(document.getElementById('one').value=='开始'){  
  35.                 document.getElementById('one').value='停止';  
  36.                 a=setInterval("fun2();",10)  
  37.             }else{  
  38.                 document.getElementById('one').value='开始';  
  39.                 clearInterval(a);  
  40.             }  
  41.         }  
  42.         //验证码  
  43.         function fun(){  
  44.             var num=document.getElementById('num').value;  
  45.             var s='';  
  46.             for(var a=1;a<=num;a++){  
  47.                 s=s+parseInt(Math.random()*10);  
  48.             }  
  49.             document.getElementById('sum').innerHTML=s;  
  50.         }  
  51.         //全选  
  52.         function fun5(){  
  53.             var q=document.getElementById('ab');  
  54.             var w=document.getElementsByName('a');  
  55.             if(q.checked==true){  
  56.                 for(var a=0;a< w.length;a++){  
  57.                     w[a].checked=false;  
  58.                 }  
  59.             }else{  
  60.                 for(var a=0;a< w.length;a++){  
  61.                     w[a].checked=true;  
  62.                 }  
  63.             }  
  64.         }  
  65.         //反选  
  66.         function fun6(){  
  67.             var e=document.getElementsByName('a');  
  68.             for(var b=0;b< e.length;b++){  
  69.                 if(e[b].checked){  
  70.                     e[b].checked=false;  
  71.                 }else{  
  72.                     e[b].checked=true;  
  73.                 }  
  74.             }  
  75.         }  
  76.     </script> 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值