基於php(Thinkphp)+jquery 實現ajax多選,反選,不選 刪除資料 新手學習向

來源:互聯網
上載者:User

標籤:數組   管理   基礎   lis   blank   恢複   新手學   xmlhttp   全選   

jquery多選源碼來源素材火http://www.sucaihuo.com/

(有什麼問題可以私本人郵箱[email protected],也可加qq來時表明來意即可)

建議有js基礎,瞭解jquery,thinkphp,廢話不說多下面就上代碼

《————HTML————》

 1 //thinkphp迴圈顯示把data裡fid賦予多選框 2 <volist name="data" id="vo"> 3 <tr> 4       <td><input type="checkbox" value="{$vo.fid}"/></td>//可在後面加td輸入參數     5 </tr> 6 </volist> 7 <tr> 8 <th width="80"><input type="checkbox" id="all"/>全選</th> 9 <th width="80"><input type="button" value="全選" class="btn" id="selectAll" /></th>10 <th width="80"><input type="button" value="全不選" class="btn" id="unSelect" /></th>11 <th><input type="button" value="反選" class="btn" id="reverse" /></th> 12 <th width="180">13   <a href="javascript:void(0);" onclick="del()" title="刪除選定資料">刪除</a>14 </th> 15 </tr>

《————jsvascript————》

 

 1 <script> 2         //多選 3         $("#all").click(function(){      4         if(this.checked){      5                 $("#list :checkbox").attr("checked", true);     6             }else{      7                 $("#list :checkbox").attr("checked", false);   8             }      9         });10         //當點到全選按鈕11         $("#selectAll").click(function () {  12            $("#list :checkbox,#all").attr("checked", true);    13         });14         //全不選15         $("#unSelect").click(function () {    16            $("#list :checkbox,#all").attr("checked", false);    17         });18         //反選19         $("#reverse").click(function () {   20             $("#list :checkbox").each(function () {    21                 $(this).attr("checked", !$(this).attr("checked"));    22             });  23             allCheck();  24         });25         //刪除26         function del(){27             var valArr = new Array;28             $("#list :checkbox[checked]").each(function(i){  29                 valArr[i] = $(this).val();  30            });31            var vals = valArr.join(‘,‘);//數群組轉換以逗號隔開的字串 32             if (valArr.length == 0) {33                 alert(‘請選擇要刪除的選項‘);34             }else{35                 if (confirm("確定刪除?刪除後將無法恢複。")){36                     var data={name:vals};37                     $.ajax({38                        type: "post",39                        url: "{:U(‘College/School/faculty_del‘)}",//url為tp方法(控制器/方法)
40 data:data,41 success: function(json) {42 var obj = eval(‘(‘ + json + ‘)‘);//返回回來的json轉化為js對象43 if (parseInt(obj.counts) > 0) {44 alert(obj.des);45 location.reload();46 } else {47 alert(obj.des);48 }49 },50 error: function(XMLHttpRequest, textStatus) {51 alert("頁面請求錯誤,請檢查重試或聯絡管理員!\n" + textStatus);52 }53 });54 }55 }56 57 }58 </script>

 《————PHP————》

 1     public function faculty_del(){ 2         $fid = trim($_POST[‘name‘]); 3         //以下為查詢條件 4         $bname[‘deletemark‘] = 0; 5         $res = $this->faculty_model
->where(array(‘fid‘=>array(‘in‘,$fid)))
->save($bname);
//查詢條件為你的查詢條件,我這邊為邏輯刪除,修改欄位值就好
6 // echo $this->faculty_model->getLastSql(); 7 // var_dump($res); 8 // exit; 9 if ($res) {10 $counts = "1";11 $des = "成功";12 } else {13 $counts = "0";14 $des = "失敗";15 }16 $json_data = "{";17 $json_data.= "\"counts\":".json_encode($counts).",";18 $json_data.= "\"des\":".json_encode($des)."";19 $json_data.= "}";20 echo $json_data;21 exit; 22 }

由於是非同步所以你發過來的資料都在控制台查看,當前頁面沒輸出,不要去頁面上去找(不要問我為什麼知道,我幹過,想起來真蠢)

基於php(Thinkphp)+jquery 實現ajax多選,反選,不選 刪除資料 新手學習向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.