问题
I used the listbox UI element in Wordpress 3.8, but it does not work in its newer version (3.9). I wrote this JS code to test it:
(function() {
var ICONS;
var icon = function(id) {
return '<i class="fa fa-' + id + '"></i>';
}
ICONS = ["rub", "ruble", "rouble", "pagelines", "stack-exchange", "arrow-circle-o-right", "arrow-circle-o-left", "caret-square-o-left", "toggle-left", "dot-circle-o", "wheelchair", "vimeo-square", "try", "adjust", "anchor", "archive", "arrows", "arrows-h", "arrows-v", "asterisk", "ban", "bar-chart-o", "barcode", "bars", "beer", "bell", "bell-o", "bolt", "book", "bookmark", "bookmark-o", "briefcase", "bug"]
var menuval=[];
for (var i = 0; i < ICONS.length; i++) {
var _id = ICONS[i];
menuval.push({text: icon( _id )+' '+_id, onclick: function(){setcontentfun(_id)}
});
}
tinymce.PluginManager.add("font_awesome_glyphs", function(editor, url) {
var menucreate=editor.addButton('fontAwesomeGlyphSelect', {
type: 'listbox',
text: 'Icons',
icon: false,
values: menuval
});
});
function setcontentfun(id) {
alert(id);
return false;
}
})();
This created displayed a listbox, but when I click on a menu item instead of alerting the last element of "ICON" array it displays all elements in listbox. How can I alert the list item, which I clicked?
回答1:
Just did some updates in your code:
var menuval=[];
var insertVar = function (val) {
return function () {
editor.insertContent(val);
}
};
for (var i = 0; i < SHORTCODES.length; i++) {
var _id = SHORTCODES[i];
var _code = SHORTCODE_EXE[i];
var variable = insertVar(_code);
//alert(_id+' '+ SHORTCODES[i]+' '+SHORTCODE_EXE[i]);
menuval.push({text: _id, onclick: variable });
}
来源:https://stackoverflow.com/questions/23265474/listbox-return-wrong-value-in-wordpress3-9