我的 WordPress 媒体 uploader 有问题,我正在选择多个图像,但问题是,我无法弄清楚如何将所有对象的键连接到单个字符串以存储到数据库中,任何帮助都会不胜感激,提前致谢...
附注我已经尝试过 $.each 但它返回一个错误,无法使用未定义的属性。
还有一件事:我在变量“attachment.url”上尝试了 console.log,这是日志:
http://localhost/wp3/wp-content/uploads/2015/01/long-shadow-logo-example.jpg
admin.js?ver=4.0.1:16 http://localhost/wp3/wp-content/uploads/2015/01/Firefox.png
admin.js?ver=4.0.1:16 http://localhost/wp3/wp-content/uploads/2015/01/fish-logo-designs-32.png
但是当我尝试将此值添加到文本框时,仅返回一个 url。
这是我的 JS 代码:
jQuery(document).on("click", ".custom_media_upload", function(e) {
e.preventDefault();
var custom_uploader = wp.media({
title: 'Select Images to Use',
button: {
text: 'Use Selected Images',
},
multiple: true // Set this to true to allow multiple files to be selected
})
custom_uploader.on('select', function() {
var selection = custom_uploader.state().get('selection');
selection.map(function(attachment) {
attachment = attachment.toJSON();
console.log(attachment.url);
});
//custom_uploader.open();
});
custom_uploader.open();
});
最佳答案
你已经使用了 jQuery(document),我认为 $.each 会起作用,如果你像这样改变 jQuery.each
关于javascript - js 对象键和值转换为单个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28207916/