在項目中,須要用到下拉框,因而找到了這個比較好用的select2插件,功能很強大,幾乎能知足全部的下拉選擇需求,選中以後能夠點擊取消選擇的,另,本例連同ajax獲取選擇的數據一併提供。javascript
<!DOCTYPE html> <html> <head> <meta charset="gb2312"> <title>添加極限活動</title> <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> <script src="http://cdn.bootcss.com/select2/4.0.2/js/select2.js"></script> <link href="http://cdn.bootcss.com/select2/4.0.2/css/select2.css" rel="stylesheet" /> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <script> $(document).ready(function(){ }); function transform(obj){ var arr = []; for(var item in obj){ arr.push(obj[item]); } return arr; } // 提交表單 function check_form() { var event_id = $.trim($('#event_id').val()); // var str = $('#sel_menu2 option:selected').val();//選中的值,只能獲取一個 var selectedValues = []; // 獲取多選的值 $("#sel_menu2 :selected").each(function(){ selectedValues.push($(this).val()); }); if(!event_id || !selectedValues) { alert('活動ID或標籤不能爲空!'); return false; } // var form_data = $('#form').serialize(); // 異步提交數據到action/add_action.php頁面 $.ajax( { url: "action/add_action.php", data:{"event_id":event_id, "tags":selectedValues,"act":"add"}, type: "post", beforeSend:function() { $("#tip").html("<span style='color:blue'>正在處理...</span>"); return true; }, success:function(data) { if(data > 0) { $("#tip").html("<span style='color:blueviolet'>恭喜,添加成功!</span>"); // document.location.href='system_notice.php' // location.reload(); } else { $("#tip").html("<span style='color:red'>失敗,請重試</span>"); alert('操做失敗'); } }, error:function() { alert('請求出錯'); }, complete:function() { $('#acting_tips').hide(); } }); return false; } </script> <body> <div class="container" width="500px;"> <form method="post" action="" class="form-horizontal" role="form" onsubmit="return check_form()" style="margin: 20px;"> <div class="form-group"> <label for="inputPassword3" class="col-sm-2 control-label">活動ID</label> <div class="col-sm-10"> <input type="text" class="form-control" name="event_id" id="event_id" placeholder="請輸入活動ID"> </div> </div> <div class="form-group"> <label for="inputPassword3" class="col-sm-2 control-label">活動標籤</label> <!-- <div class="col-sm-10"> <input type="text" class="form-control" id="tags" placeholder="回車肯定"> </div> --> <div class="col-sm-10"> <select id="sel_menu2" multiple="multiple" name="mytags" class="form-control"> <optgroup label="選擇標籤"> <option value="1">用戶管理</option> <option value="2">角色管理</option> <option value="3">部門管理</option> <option value="4">菜單管理</option> </optgroup> </select> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">添加</button> <span id="tip"></span> </div> </div> </form> </div> <script type="text/javascript"> //多選 $("#sel_menu2").select2({ tags: true, // allowClear: true, maximumSelectionLength: 5 //最多可以選擇的個數 }); </script> </body> </html>
###action/add_action.php接收頁面php
<?php /** * 獲取提交的數據 * */ // 獲取參數 $tags = $_POST['tags']; $event_id = $_POST['event_id']; // 在該頁面處理以後,返回數據 echo 1;
##列表頁面 css
###列表html頁面html
<!DOCTYPE html> <html> <head> <meta charset="gb2312"> <title>Demo活動列表</title> <link href="http://libs.baidu.com/bootstrap/3.0.3/css/bootstrap.min.css" rel="stylesheet"> <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> <script src="http://libs.baidu.com/bootstrap/3.0.3/js/bootstrap.min.js"></script> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <script> // 提交表單 function delete_event(event_id) { if(!event_id) { alert('Error!'); return false; } // var form_data = new Array(); $.ajax( { url: "action/event_action.php", data:{"event_id":event_id, "act":"del"}, type: "post", beforeSend:function() { $("#tip").html("<span style='color:blue'>正在處理...</span>"); return true; }, success:function(data) { if(data > 0) { alert('操做成功'); $("#tip").html("<span style='color:blueviolet'>恭喜,刪除成功!</span>"); // document.location.href='world_system_notice.php' location.reload(); } else { $("#tip").html("<span style='color:red'>失敗,請重試</span>"); alert('操做失敗'); } }, error:function() { alert('請求出錯'); }, complete:function() { // $('#tips').hide(); } }); return false; } </script> <body> <div class="container"> <table class="table table-hover table-bordered" style="margin: 20px;"> <caption>列表頁面Demo</caption> <thead> <tr> <th>id</th> <th>活動ID</th> <th>標籤</th> <th>編輯ID</th> <th>添加時間</th> <th>操做</th> </tr> </thead> <tbody> <tr> <td>1</td> <td>123456</td> <td>ios,php,Laravel</td> <td>15698762</td> <td>2016-06-01 18:14:12</td> <td><button type="button" class="btn btn-danger" onclick="return delete_event({event_id})">刪除</button></td> </tr> </tbody> </table> <!-- 此處能夠添加分頁 --> </span> </div> </body> </html> </html>