我正在WordPress编辑器中创建一个按钮,现在一切正常,但我的数组中有一些动态数据,想要使用该数组。场景是这样的,我想将该数组的键作为下拉名称,而我将使用该数组的值作为该下拉列表的值。 到目前为止,这是我按钮的代码,工作正常,
editor.windowManager.open( {
title: 'Insert header tag',
body: [{
type: 'textbox',
name: 'title',
label: 'Your title'
},
{
type: 'textbox',
name: 'id',
label: 'Header anchor'
},
{
type: 'listbox',
name: 'level',
label: 'Header level',
'values': [
/*{text: '<h3>', value: '3'},
{text: '<h4>', value: '4'},
{text: '<h5>', value: '5'},
{text: '<h6>', value: '6'}*/
jQuery.each(ajaxResult, function(obj, values) {
console.log('its from that key');
{text: obj, value: values},
})
]
}],
onsubmit: function( e ) {
editor.insertContent( '<h' + e.data.level + ' id="' + e.data.id + '">' + e.data.title + '</h' + e.data.level + '>');
}
});
}
我想使用这个数组代码作为我的列表,但它在控制台中说出了意想不到的事情:虽然我觉得很好。这是我打印该数组值的每个函数
jQuery.each(ajaxResult, function(obj, values)
console.log('its from that key');
{text: obj, value: values},
})
它应该只打印它的钥匙代替obj及其价值代替价值......这就是它的意图,但它给了我那个错误。任何人都可以指导我哪里出错了???请任何人帮我解决这个问题......这是我在控制台中打印的数组数据。
{"another":"another","Uncategorized":"uncategorized"}
我只想在我上面的按钮点击功能中将“另一个”作为键及其值作为值。但它没有任何人指导我吗?
注意: ajaxResult
是我正在使用的数组名称。
注意:这个静态的注释代码工作正常,但我希望它替换为数组中的动态数据。
/*{text: '<h3>', value: '3'},
{text: '<h4>', value: '4'},
{text: '<h5>', value: '5'},
{text: '<h6>', value: '6'}*/
答案 0 :(得分:1)
问题是,您尝试在数组声明中运行each
。那样不行。
如果您的ajaxResult
看起来像{"another":"another","Uncategorized":"uncategorized"}
,那么您只需
var values=[];
jQuery.each(ajaxResult, function(text, value)
{
values.push({text: text, value: value})
})
然后,只需将values
分配给'values'
属性,就像这样
'values': values