这个问题是因为第一个“if”,好的文本字段中只设置了第一次
要改变这一点,您可以在HTML中添加一个“data”参数,并像这样更改JavaScript代码(查看中的修改meta_image_frame.on(\'select\'
...)
(function($) {
\'use strict\';
$(function() {
var meta_image_frame;
function saveImage(e, uiElement) {
e.preventDefault();
if (meta_image_frame) {
meta_image_frame.field = $("#" + $(e.target).data("field"));
meta_image_frame.open();
return;
}
meta_image_frame = wp.media.frames.meta_image_frame = wp.media({
title : "meta_image.title",
button : {
text : "meta_image.button"
},
library : {
type : \'image\'
}
});
meta_image_frame.on(\'select\', function() {
var media_attachment = meta_image_frame.state()
.get(\'selection\').first().toJSON();
meta_image_frame.field.val(media_attachment.url);
});
// Opens the media library frame.
meta_image_frame.open();
}
$(\'#fa_img1-button\').click(function(e) {
saveImage(e, \'#fa_img1\');
});
$(\'#fa_img2-button\').click(function(e) {
saveImage(e, \'#fa_img2\');
});
});
})(jQuery);
您可以将其与以下HTML代码一起使用:
<input id="fa_img1" type="text" value=""/>
<br/>
<input id="fa_img1-button" type="button" data-field="fa_img1"
value="Upload Image"/>
<br/>
<br/>
<input id="fa_img2" type="text" value=""/>
<br/>
<input id="fa_img2-button" type="button" data-field="fa_img2"
value="Upload Image"/>
<br/>
<br/>