2016-08-12 106 views
1

我的問題是關於獲取全局變量分配給項目。ajax全局變量與jquery

var cat; // clicked menu , reference for category, global variable 

$(window).ready(function() { 


    jQuery(function() { 

     $('li.menuItem').click(function(e) { 

      cat = $('a', this).text(); 



      $.ajax({ 
       type: "GET", 
       timeout: 30000, 
       async: false, 
       cache: false, 
       global: false, 
       dataType: "text", 
       url: 'items.php?genre=' + cat, 
       success:function (data) { 
       items(data);// output number of items handled by function items() to make it global 
       } 
      }); 




     }); 
    }); 

function items(data) { 
// We want this to show the "results" from the callback function.     

    console.log(data); // <-- this gives me the right number of items eatch time i click on different category 
     } 

    $('.pagination').pagination({ 


     items: ??, <-- my issue is to assign the output data on the fly here, 
     itemsOnPage: 20, 
     cssStyle: 'light-theme', 
     currentPage: 1, 
     onPageClick: function(pageNumber) { 
      jQuery(".movies-list").html('loading...'); 
      jQuery(".movies-list").load("connect.php?genre=" + cat + "&page=" + pageNumber); 

     } 
    }); 
}); 

我無法弄清楚如何現在解決它是一個星期或更長時間的谷歌搜索,並沒有成功在計算器上衝浪。 預先感謝您

+1

,其是用於分頁插件,是它的[simplePagination](http://flaviusmatis.github.io/simplePagination.js/) –

+0

刪除 「}」 的console.log(數據)之後再更換??與數據 –

+0

也會很好,如果你告訴我們你console.log結果 –

回答

0

看起來你正在使用的simplePagination.js,如果是的話,你可以使用updateItems方法來更新項目

function items(data) { 
    $('.pagination').pagination('updateItems', data); 
} 
+0

你可以更新我的代碼請問? –

+0

@Jakozi不知道這是什麼意思?只需將你的函數替換爲我在上面發佈的函數,看看它是否正在工作 –

+0

就像這樣:function items(data){//我們希望這個函數顯示回調函數中的「結果」。 $('。pagination')。pagination('updateItems',data); } $( '分頁')分頁({ 項目:222, itemsOnPage:20, cssStyle: '光主題', 當前頁:1, onPageClick:功能(PAGENUMBER) jQuery(「。movies-list」)。html('loading ...'); jQuery(「。movies-list」)。load(「connect.php?genre =」+ cat +「&page =」 + pageNumber); } }); }); –

0

是真的,我有一些外部引用這樣的IM張貼他們在這裏的數:

"maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" 
"ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.js" 
"maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js" 
"phpflow.com/demo/jquery_ajax_pagination_with_php_demo/dist/simplePagination.css" 
"phpflow.com/demo/jquery_ajax_pagination_with_php_demo/dist/jquery.simplePagination.js" 
"fontawesome.io/assets/font-awesome/css/font-awesome.css" 
"seegatesite.com/bootstrap/css/bootstrap.min.css" 
"seegatesite.com/bootstrap/css/simple-sidebar.css" 
"seegatesite.com/bootstrap/js/jquery-1.11.2.min.js"