javascript - jquery 的 ajax 全局变量

标签 javascript 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);

        }
    });
});

我不知道如何解决,现在已经在 StackOverflow 上进行了一周或更长时间的谷歌搜索和冲浪,但没有成功。 预先感谢您

最佳答案

看起来您正在使用 simplePagination.js ,如果是这样,您可以使用 updateItems 方法来更新项目数量

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

关于javascript - jquery 的 ajax 全局变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38909386/

相关文章:

javascript - 在 JavaScript 中强制刷新 DOM

javascript - 创建 CSS 网格

javascript - 从按钮中删除 ng-grid 表中的行

javascript - datepicker 在 Internet Explorer 8 中是透明的

javascript - 使用 AcornJS 中的同步回调处理程序远离全局状态

javascript - ESLint/Prettier -- 强制执行 max-len/printWidth,但不需要它?

Javascript 错误 - 未定义

javascript - 在 .each 循环期间未显示 Div 类更改

jQuery 通过数据类型选择

jquery - 获取选定的单元格元素数据表