0

这是我通过 AJAX 动态编写的提交按钮:

var htmlpage = "<div class='pages'>"
                for (i=1 ; i < pages+1 ; i++)
                    {
                        htmlpage += "<li><input type='submit' value='"+i+"' onclick='updatefilters;' /></li"
                    }
                htmlpage += "<div>"    
                htmlpage += "</ul>";

我正在尝试重新运行 updatefilters() 函数来更改显示的项目。我想在没有看到所有代码的情况下概念化有点困难......但基本上,我需要做的就是在每次单击提交按钮时再次运行该函数......现在,它给了我一个updatefilters is undefined萤火虫错误。

这是我的整个 JS 供参考

$(function() {
    $( "#selectable" ).selectable({
        selected: updatefilters  
    });
    getactivesession();
    function getactivesession(ev, ui){
        var i = 0;
        var actfilter, strfilter;
        var strfilterarray = new Array();
        $.ajaxSetup({cache: false})
        $.ajax({
        type: "POST",  
        async: false,
        url: 'welcome/getactivesession', 
        dataType: 'json',
        success: function (data){
            strfilter = JSON.stringify(data)
            strfilterarray = strfilter.split(',')
            for (i=0 ; i < strfilterarray.length ; i++) {
                strfilter = strfilterarray[i]
                strfilter = strfilter.replace(/[\[\]'"]+/g,'');
                var strfilterdash = strfilter.replace(/\s+/g, '-')
                actfilter = '#'+ strfilterdash
                $(actfilter).addClass('ui-selected')
            }
            updatefilters();
        }
        });
    }

    function updatefilters(ev, ui){
        // get the selected filters
        var template, html;
        var i = 0;
        var page;
        if(! page){
            page = 0; 
        }

        var $selected = $('#selectable').children('.ui-selected');
        // create a string that has each filter separated by a pipe ("|")
        var filters = $selected.map(function(){return this.id;}).get().join("\|");
        $.ajax({
            type: "POST",
            async: false,
            url: 'welcome/updatefilters',
            dataType: 'json', 
            data: { filters: filters, page: page },
            success: function(data){
                var html = "";
                html += "<div id=board>"
                html += "<div class='board' id='table'>"
                html += "<div id='row'>header here</div>"
                var pages = Math.ceil(data['num_threads']/10);
                var htmlpage = "<div class='pages'>"
                for (i=1 ; i < pages+1 ; i++)
                    {
                        htmlpage += "<li><input type='submit' value='"+i+"' onclick='updatefilters;' /></li"
                    }
                htmlpage += "<div>"    
                htmlpage += "</ul>";
                htmlpage += "</br>";
                html += htmlpage;
                for (i=0 ; i < data['threads'].length ; i++)
                {
                    html += "<div id=row>";
                    html += "   <div id='author' style='background: url("+data['threads'][i].location + ") no-repeat; background-position: center;'><p>"+data['threads'][i].username + "</p></div>";
                    html += "   <div id='arrow'></div>";
                    html += "   <div id='subject' title='"+ data['threads'][i].body +"'>";
                    html += "       <a href=thread/" + data['threads'][i].slug + ">"+ data['threads'][i].subject +"</a><p>Created: "+data['threads'][i].posttime+"</p></div>";                    
                    html += "   <div id='info'>";
                    html += "       <div id='replies'>" + data['threads'][i].replies_num + "</div>";
                    html += "       <div id='lastpost'>"+ data['threads'][i].lastreply+"</div>";
                    html += "   </div>";
                    html += "</div>";
                }
                html += "</div></div>";
                $('#board').html(html);
            }
        });
    } 
});
4

1 回答 1

1

这种方法似乎存在一些问题。

首先,您实际上并没有在onclick处理程序中调用该函数。

htmlpage += "<li><input type='submit' value='"+i+"' onclick='updatefilters;' /></li"

应该:

htmlpage += "<li><input type='submit' value='"+i+"' onclick='updatefilters();' /></li"

其次,updatefilters无法从全局范围访问该函数,而全局范围是执行该匿名函数的地方。您必须移到回调function updatefilters(ev, ui)之外onload,也许是脚本块的顶部。

于 2012-06-11T16:31:11.070 回答