JS多個函數之間傳遞參數的一個重要思想是在頁面定義一個隱藏域,當第一個函數請求到數據時候修改隱藏域的值,第二個函數用jQuery的選擇器選擇頁面中隱藏域的值。
比如:
頁面中定義一個隱藏的頁號。
<!-- 隱藏查詢條件的頁號 -->
<input type="hidden" name="currentPage" id="currentPage">
第一個ajax函數獲取頁面中的頁號:
function queryNum(checkunit, dangergrade, type) { alert("點擊查詢按鈕條件" + checkunit + " " + dangergrade + " " + type); $.ajax({ url : "/danger/queryDangerTongji.action", async : true, data : { "currentPage" : $("#currentPage").val(), // 查詢隱藏的頁號 "checkunit" : checkunit, "dangergrade" : dangergrade, "type" : type }, dataType : "text", type : "POST", success : showTable, error : function() { alert("請求失敗!"); } }); }
第二個函數給隱藏的頁號賦值(下次點擊頁號的時候就可以通過一個隱藏域獲取到值)
function page(currentPage, totalCount, checkunit, type, dangergrade) { // 修改分頁的基本屬性 $('#paginationIDU1').pagination( { // 組件屬性 "total" : totalCount,// 數字 當分頁建立時設置記錄的總數量 1 "pageSize" : 8,// 數字 每一頁顯示的數量 10 "pageNumber" : currentPage,// 數字 當分頁建立時,顯示的頁數 1 "pageList" : [ 8 ],// 數組 用戶可以修改每一頁的大小, // 功能 "layout" : [ 'list', 'sep', 'first', 'prev', 'manual', 'next', 'last', 'links' ], "onSelectPage" : function(pageNumber, b) { alert("查詢后條件:" + currentPage + " " + totalCount + " " + checkunit + " " + type + " " + dangergrade); // queryNum(checkunit, dangergrade, type, currentPage); $("#currentPage").val(pageNumber); alert($("#currentPage").val());// 向頁面的隱藏域設置一個值 queryNum(checkunit, type, dangergrade); } }); }
另外:在傳遞字符串參數的時候需要加引號,在JS拼接的時候也需要加引號
例如:
"<a class='button' href=javascript:void(0) onclick='deleteCw(\""+list[i]+"\")'><span class='glyphicon glyphicon-trash'></span></a>"
\"代表轉義字符,結果是作為"處理。
