每个循环中的jQuery / JSON对象

时间:2016-04-16 16:40:32

标签: jquery json wordpress

我目前正在尝试填充选择时创建的空白字段。 (从媒体模式中选择图像)。 关闭媒体模式后,将创建新的空字段并保存选择。 为了获得不同的数据(不同的图像大小),我必须使用json,尽管我对json很新,这是我获取数据的唯一方法。

现在我尝试使用我从选择中获得的数据填充空白字段,但所有字段仅由最后一个URL填充..

// 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  

0 个答案:

没有答案