Eval函数或document.write

时间:2015-04-24 12:05:26

标签: javascript jquery

我使用插件http://tablesorter.com/docs/在javascript中对我的表进行排序并过滤列我的问题是我不想在所有列上放置过滤器,所以我使用它:

           widgetOptions : {
                filter_formatter : {
                    1:function($cellnindx){return null;},
                    4:function($cell,indx){return null;}
                }
            }

它的工作,但现在我想做一个脚本,不要在列上打印过滤器,这个列有notFiler类,所以我做这个脚本:

        var temp = "";
        var i = 0;
        $(".tablesorter th").each(function()
        {
            if($(this).attr('class') == "notFiler")
            {
                if(temp != "")
                {
                    temp += ',';
                }
                temp += i + ': function($cell, indx){return null;}';
            }
            i++;
        });

它允许我生成我允许我不打印过滤器的文本,但是当我这样做时:

        widgetOptions : {
            filter_formatter : {
                eval(temp);

            }
        }

我有一个javascript错误:意外的令牌;

2 个答案:

答案 0 :(得分:1)

不要使用eval。你不能简单地这样做吗?

首先创建结果对象

var widgetOptions = {
    filter_formatter: {};
};

现在只需附加

$(".tablesorter th").each(function(i) {
    if($(this).attr('class') == "notFiler") {
        widgetOptions.filter_formatter[i] = function($cell, indx){return null;};
    }
});

然后,初始化tablesorter

var tablesorter = $('.tablesorter').tablesorter({
    widgetOptions: widgetOptions
});

答案 1 :(得分:0)

eval(temp)位于JavaScript对象的一侧,不允许使用分号表示行或项的结尾。尽管看起来令人沮丧,但从该行中删除分号并以一行结束该行。