我目前正在开发一个 ckeditor 插件,选择框从数据库中获取数据,我只能让第一个字符出现在选择框中。
get_pages.php
$query = selectQuery('
SELECT title, pageID
FROM `page_info`
WHERE partnerID =?',array($partnerId));
$test = '';
foreach ($query as $key => $value) {
$test .= $value['title'].",";
}
Plugin.js
var pages = $.post(
"/action/ac_select_pages.php",
{ pid: "1" },
function(data) {
return (data);
}
);
pages = pages.responseText.split(',');
我的变量:
pages: Array[31]
0: "Home"
1: "Control Panel"
2: "24/7 Support"
3: "Script / Databases"
4: "Last Announcment"
5: "E-mail: No Limit"
6: "Webmail & Push Mail"
等等..
我如何制作我的选择框:
{
type : 'select',
id : 'moreinfo',
label : 'Meerinfo Link',
style : 'width:300px;',
items : pages ,
setup : function( element )
{
this.setValue( element.getAttribute( "moreinfo" ) );
},
commit : function( element )
{
var id = this.getValue();
// If the field is non-empty, use its value to set the element's id attribute.
if ( id )
element.setAttribute( 'moreinfo', id );
// If on editing the value was removed by the user, the id attribute needs to be removed.
// http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.dom.element.html#removeAttribute
else if ( !this.insertMode )
element.removeAttribute( 'moreinfo' );
}
}
如您所见,我只是将数组放在项目中,但它只显示第一个字符
那我做错了什么?
最佳答案
所以,总结一下......
split()
通过将字符串分成子字符串,将字符串对象拆分为字符串数组。
因此,大概您的 pages.responseText
看起来像这样:
Home,Control Panel,24/7 Support,Script / Databases,Last Announcment,E-mail: No Limit,Webmail & Push Mail
这意味着在执行 pages = pages.responseText.split(',');
之后,pages
看起来像这样(一个数组):
["Home", "Control Panel", "24/7 Support", "Script / Databases", "Last Announcment", "E-mail: No Limit", "Webmail & Push Mail"]
根据您的previous comment ,这有效:
var pages = [{0: 'Home', 1: 'Control Panel', ...}];
如您所见(具有多个值的数组):
["Home", "Control Panel", ...]
不与(一个对象具有多个属性的数组)相同:
[{0: 'Home', 1: 'Control Panel', ...}]
如果所有这些仍然正确,那么这样的事情应该可行:
var pagesObject = {};
for (var i=0, max=pages.length; i<max; i += 1) {
pagesObject[i] = pages[i];
}
var correctPages = [ pagesObject ];
关于javascript数组只显示第一个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14939193/