laravel框架之即點即改
1 //控制器層 2 3 public function ajaxsex(request $request) 4 { 5 $id = $request->get('id'); 6 $fd = $request->get('fd'); 7 if ($fd=="男"){ 8 $fd="女"; 9 DB::table("users")->where('id',$id)->update(['sex'=>1]); 10 echo json_encode($fd); 11 }else { 12 $fd="男"; 13 DB::table("users")->where('id',$id)->update(['sex'=>0]); 14 echo json_encode($fd); 15 } 16 }
1 //視圖層 2 3 <tbody id="tb"> 4 @foreach($data as $key=>$val) 5 <tr id="{{$val->id}}"> 6 <td><input type="checkbox" name="check" value="{{$val->id}}"></td> 7 <td>{{$val->id}}</td> 8 <td fd="username">{{$val->username}}</td> 9 @if($val->sex == 0 ) 10 <td id="{{$val->id}}" class="se" fd="{{$val->sex}}">男</td> 11 @elseif($val->sex == 1) 12 <td id="{{$val->id}}" class="se" fd="{{$val->sex}}">女</td> 13 @endif 14 <td fd="email">{{$val->email}}</td> 15 <td>{{$val->created_at}}</td> 16 <td>{{$val->updated_at}}</td> 17 <td><a href="javascript:void (0)" id="{{$val->id}}" class="del">刪除</a>|<a href="update?id={{$val->id }}">編輯</a></td> 18 </tr> 19 @endforeach 20 </tbody> 21 22 //即點即改 23 $(document).on("click",".se",function () { 24 var id = $(this).attr('id'); 25 26 var fd = $(this).attr('fd'); 27 var that = $(this); 28 var str = ""; 29 if (fd==0){ 30 str="男"; 31 } else { 32 str = "女"; 33 } 34 $.ajax({ 35 url:"ajaxsex", 36 type:"get", 37 dataType:"json", 38 data:{ 39 id:id, 40 fd:str, 41 }, 42 success:function (data) { 43 console.log(data); 44 if (data=="男"){ 45 that.text("男"); 46 that.attr('fd',0) 47 } else { 48 that.text("女"); 49 that.attr('fd',1) 50 } 51 } 52 }) 53 })