每个循环中的jQuery/JSON对象
我目前正在尝试填充选择时创建的空字段。(从媒体模式中选择图像)。 关闭介质模式后,将创建新的空字段并保存所选内容。 为了获得不同的数据(不同的图像大小),我不得不使用json,尽管我对json非常陌生,但这是我获取数据的唯一方法 现在,我试图用我从选择中获得的数据填充空字段,但所有字段仅由最后一个URL填充每个循环中的jQuery/JSON对象,jquery,json,wordpress,Jquery,Json,Wordpress,我目前正在尝试填充选择时创建的空字段。(从媒体模式中选择图像)。 关闭介质模式后,将创建新的空字段并保存所选内容。 为了获得不同的数据(不同的图像大小),我不得不使用json,尽管我对json非常陌生,但这是我获取数据的唯一方法 现在,我试图用我从选择中获得的数据填充空字段,但所有字段仅由最后一个URL填充 // When files is selected mediaUploader.on('select', function() { //Get th
// When files is selected
mediaUploader.on('select', function() {
//Get the selection from the modal..
selection = mediaUploader.state().get('selection');
//Get the amount of selections made in the Media Modal...
selection_count = selection.length;
//Simulate "Add new images" the selection amount of times..
for(var i = 0; i < selection_count; i++) {
jQuery(theMetabox).find('.js-wpt-repadd').trigger('click');
}
//Map it...
selection.map( function( attachment ) {
//JSONIFY THE ATTACHMENTS
attachment = attachment.toJSON();
//Get the thumbnail size URL from JSON
var thumbnailUrl = attachment.sizes.thumbnail.url;
//Get the full size URL from JSON
var fullUrl = attachment.url;
//LOG IT IN CONSOLE FOR TESTING (Works perfect..)
console.log(thumbnailUrl);//Outputs all thumbnail URLS in console
console.log(fullUrl);//Outputs all full URLS in console
jQuery('.wpt-form-textfield[value=""]').each( function(index) {
//Mark the new fields with red border (testing.. working...)
jQuery(this).css('border','2px solid red');
jQuery(this).val(fullUrl[index]); // <--- This is the issue, this returns the index number of the fullUrl instead of the url that it shows in the console..
});
});
//Reset selection..
selection = null;
}); //END ON SELECT IMAGES
//选择文件时
mediaUploader.on('select',function()){
//从模式中获取选择。。
selection=mediaUploader.state().get('selection');
//获取在媒体模式中所做选择的数量。。。
选择计数=选择长度;
//模拟“添加新图像”选择次数。。
对于(变量i=0;i映射中的每个循环,这本身就是一种循环方法谢谢您的建议:)我现在已经最小化了代码。我不确定我当时是否完全理解了.map函数。我可以将每个函数移到map之外,因为填充发生在之后,但是我不能使用我的变量“fullPath”,它说它当时未定义。。