jQuery替代PHP foreach()

时间:2012-05-16 06:07:13

标签: jquery json

当我像这样一个一个地手动推送params:aoData.push({“name”:“process”,“value”:“fetch”});它运行良好,没有任何错误消息。

但是,现在我想,在脚本顶部声明数组tableXtraPostParams并将这个数组的参数逐个推入aoData

这段代码工作正常

var tableXtraPostParams = {
    "process":"fetch",
    "which":"mine"
};
$.each(tableXtraPostParams, function(n, v){
    aoData.push( {
       "name": n, 
       "value": v
    } );
});

但我在JS日志中收到错误消息

语法错误,无法识别的表达式:[object Object]

enter image description here

我在这里缺少什么?

任何建议都将不胜感激!

更新

我知道这个错误与我的修改有关。 (因为当我删除$.each时,我不会收到任何此类错误)

这是Aodata在推送之前的样子

[Object { name="sEcho", value=1}, Object { name="iColumns", value=8}, Object { name="sColumns", value=""}, Object { name="iDisplayStart", value=0}, Object { name="iDisplayLength", value=25}, Object { name="mDataProp_0", value=0}, Object { name="mDataProp_1", value=1}, Object { name="mDataProp_2", value=2}, Object { name="mDataProp_3", value=3}, Object { name="mDataProp_4", value=4}, Object { name="mDataProp_5", value=5}, Object { name="mDataProp_6", value=6}, Object { name="mDataProp_7", value=7}, Object { name="sSearch", value=""}, Object { name="bRegex", value=false}, Object { name="sSearch_0", value=""}, Object { name="bRegex_0", value=false}, Object { name="bSearchable_0", value=true}, Object { name="sSearch_1", value=""}, Object { name="bRegex_1", value=false}, Object { name="bSearchable_1", value=true}, Object { name="sSearch_2", value=""}, Object { name="bRegex_2", value=false}, Object { name="bSearchable_2", value=true}, Object { name="sSearch_3", value=""}, Object { name="bRegex_3", value=false}, Object { name="bSearchable_3", value=true}, Object { name="sSearch_4", value=""}, Object { name="bRegex_4", value=false}, Object { name="bSearchable_4", value=true}, Object { name="sSearch_5", value=""}, Object { name="bRegex_5", value=false}, Object { name="bSearchable_5", value=true}, Object { name="sSearch_6", value=""}, Object { name="bRegex_6", value=false}, Object { name="bSearchable_6", value=true}, Object { name="sSearch_7", value=""}, Object { name="bRegex_7", value=false}, Object { name="bSearchable_7", value=true}, Object { name="iSortingCols", value=1}, Object { name="iSortCol_0", value=0}, Object { name="sSortDir_0", value="asc"}, Object { name="bSortable_0", value=true}, Object { name="bSortable_1", value=true}, Object { name="bSortable_2", value=true}, Object { name="bSortable_3", value=true}, Object { name="bSortable_4", value=true}, Object { name="bSortable_5", value=true}, Object { name="bSortable_6", value=true}, Object { name="bSortable_7", value=true}]

这是推后

[Object { name="sEcho", value=1}, Object { name="iColumns", value=8}, Object { name="sColumns", value=""}, Object { name="iDisplayStart", value=0}, Object { name="iDisplayLength", value=25}, Object { name="mDataProp_0", value=0}, Object { name="mDataProp_1", value=1}, Object { name="mDataProp_2", value=2}, Object { name="mDataProp_3", value=3}, Object { name="mDataProp_4", value=4}, Object { name="mDataProp_5", value=5}, Object { name="mDataProp_6", value=6}, Object { name="mDataProp_7", value=7}, Object { name="sSearch", value=""}, Object { name="bRegex", value=false}, Object { name="sSearch_0", value=""}, Object { name="bRegex_0", value=false}, Object { name="bSearchable_0", value=true}, Object { name="sSearch_1", value=""}, Object { name="bRegex_1", value=false}, Object { name="bSearchable_1", value=true}, Object { name="sSearch_2", value=""}, Object { name="bRegex_2", value=false}, Object { name="bSearchable_2", value=true}, Object { name="sSearch_3", value=""}, Object { name="bRegex_3", value=false}, Object { name="bSearchable_3", value=true}, Object { name="sSearch_4", value=""}, Object { name="bRegex_4", value=false}, Object { name="bSearchable_4", value=true}, Object { name="sSearch_5", value=""}, Object { name="bRegex_5", value=false}, Object { name="bSearchable_5", value=true}, Object { name="sSearch_6", value=""}, Object { name="bRegex_6", value=false}, Object { name="bSearchable_6", value=true}, Object { name="sSearch_7", value=""}, Object { name="bRegex_7", value=false}, Object { name="bSearchable_7", value=true}, Object { name="iSortingCols", value=1}, Object { name="iSortCol_0", value=0}, Object { name="sSortDir_0", value="asc"}, Object { name="bSortable_0", value=true}, Object { name="bSortable_1", value=true}, Object { name="bSortable_2", value=true}, Object { name="bSortable_3", value=true}, Object { name="bSortable_4", value=true}, Object { name="bSortable_5", value=true}, Object { name="bSortable_6", value=true}, Object { name="bSortable_7", value=true}, Object { name="process", value="fetch"}, Object { name="which", value="all"}]

3 个答案:

答案 0 :(得分:0)

你是否包含了datatables.js文件?例如,

<script type="text/javascript" src="/js/dataTables/jquery.dataTables.js"></script>

答案 1 :(得分:0)

该错误与您的代码无关,除非您实际将自己的代码放入jquery.js。

但是具有该名称的文件已损坏。你修改了那个文件中的任何内容吗?如果没有,只需使用http://jquery.com/

中的新副本进行恢复

答案 2 :(得分:0)

使用tableExtraParams.foreach。并且$ .each的第二个参数function (k, v)不起作用。它需要两个参数, index key 。 k将等于0,1,2,3等,v将等于“process”,“which”等,如果你想获得键/值对,请使用

var tableXtraPostParams = {
    "process":"fetch",
     "which":"mine"
};
$.each(tableXtraPostParams, function(n, v){
    aoData.push({
        "name": v, 
        "value": tableXtraPostParams[v]
    });
});