1

我正在开发一个 wordpress 博客,在每篇文章的编辑页面上都有一个自定义元框。
此元框由表组成,每行包含从媒体库中选择的图像 src。

现在添加的每个新行都有一个 id:
第 1 行:img_metabox_src_0
第 2 行:img_metabox_src_1
第 3 行:img_metabox_src_2

表头如下:


----Image < img >------ |----- URL(输入文本框)------ | -------- 选择图片(输入提交)------ | -----删除图片(输入提交)--------


现在,在任何行上单击“选择图像”时,我从 jquery 中检索行索引,然后将:“img_metabox_src_”+index 发送到 file_frame.on('select', function() 以进行 url 更新。

IE

 jQuery('tr #select_image').off().on('click', function( event ){

event.preventDefault();


var row_index = jQuery(this).closest('tr').index();
var id = "img_metabox_src_" + row_index;

//******** 1 ***********
console.log('row_index');
console.log(row_index);

console.log(id);
console.log(jQuery('#' + id));


if ( file_frame ) {
file_frame.open();
return;
}


file_frame = wp.media.frames.file_frame = wp.media({
                                        title: "Select/Upload Image",
                                        button: {
                                                text: "Select",
                                                },
                                        library : { type : 'image'},
                                        multiple: false
                                       });


file_frame.on( 'select', function() {
                         attachment = file_frame.state().get('selection').first().toJSON();

                         // "mca_features_tray" is the ID of my text field that will receive the image
                         // I'm getting the ID rather than the URL:

                         // but you could get the URL instead by doing something like this:
                         //******** 2 ***********
                         console.log(id);
                         console.log(jQuery('#' + id));
                         jQuery('#' + id).attr('value',attachment.url);
                         id = null;
});

现在,

案例 1:当我第一次单击行 index3 时,URL 会在 img_metabox_src_3 上更新。

案例 2:但是在那之后,无论我点击哪一行,img_metabox_src_3 上的 url 都会更新。

同样在添加日志时,我得到

(对于案例 2,假设我单击了行索引 1):

//******** 1 ***********
行索引:1
id:img_metabox_src_1

//******** 2 ***********
id : img_metabox_src_3

即在file_frame.on('select', function() {内,
ID 值更改为第一次点击的值。

请帮助了解如何将更新的行索引/ID 传递给选择函数

4

1 回答 1

0

谢谢,我使用了全球概念:

function set_row_index (ind){
row_index = ind;
}

function get_row_index(){
return row_index;
}

jQuery(document).ready(function(){

jQuery('tr input.select_media_library').off().on('click', function( event ){

event.preventDefault();
var index = jQuery(this).closest('tr').index();
**set_row_index(index);**
.
.
.

file_frame.on( 'select', function() {
                         attachment = file_frame.state().get('selection').first().toJSON();
                         **index = get_row_index();**
                         var id = "img_src_" + index;
                         jQuery('#' + id).attr('value',attachment.url);
                  });
file_frame.open();
});
于 2016-08-30T07:15:00.747 回答