• notice
  • Congratulations on the launch of the Sought Tech site

layui-switch changes state dynamically

The first part, download the layui package from the official website, recommend git ( portal ), how to introduce the project, let’s not say how to enter the theme

The environment is php+mysql+js

html page part

<form class = "layui-form" style = " height : 30px ; float : left " >   
  <div class = "layui-form-item" >
      <div class = "layui-input-block" style = "" >  
           <input type = "checkbox" lay-skin = "switch" {$ vo.status ? "checked=''" : "" } lay-filter = "switch" data-url = "{:url('game_list/changeShow ',array('id'=> $vo['id']))}" lay-text="on|off">  
      </div>
  </div>
</form>

js part:

//Add switch monitor
layui.use ( 'form' , function (){
       var form = layui.form ;
       form.on ( 'switch(switch)' , function ( data ) {  
           var url = $ ( this ).data ( 'url' );
           var status = data.elem.checked ; //Whether the switch is on, true or false
           //What I need in the background is 0 or 1, so the value of change is processed in js in advance
           if ( status ) {
               status = 1 ;
           } else {  
               status = 0 ;
           }
           $.post ( url , { status : status }, function ( res ) {  
               if ( res.code ) {
                   Layer.MSG ( RES.MSG );
               }
           });
       });
   });
//Change database status
public function changeShow ()
   {
       $GameList = new GModel ();  
       if ( $this -> request -> isPost ()) {  
           $data [ 'id' ] = $this -> request -> param ( 'id' );
           $data [ 'status' ] = $this -> request -> param ( 'status' );

           $data = $GameList -> allowField ( true ) -> update ( $data );
           if ( $data [ 'status' ]== 1 ) {  
               $msg = 'Game opened successfully' ;
               return json ([ 'code' => 1 , 'msg' => $msg ]);
           } elseif ( $data [ 'status' ]== 0 ){
               $msg = 'Game closed successfully' ;
               return json ([ 'code' => 1 , 'msg' => $msg ]);
           }
       } else {
           $res [ 'code' ] = 0 ;  
           $res [ 'msg' ] = 'This is an accident! ' ;  
           return $res ;
       }
   }

Okay, that’s all.// Mainly because the php on the page is not worth seeing, it will update at all

Tags

Technical otaku

Sought technology together

Related Topic

0 Comments

Leave a Reply

+