如何:jQuery多个WordPress媒体上传器按钮在同一选项页面?

时间:2016-12-05 作者:Lola Bittencourt


我想在我的主题选项页面中使用按钮调用wordpress媒体上传程序。问题是,我需要在同一页上有三个上传媒体按钮。我正试图使用jQuery多ID选择器来实现这一点。代码工作正常:我单击按钮,启动媒体上传程序,但是当我将任何内容上传到第一个输入字段时,我刚刚上传的媒体会传递到页面中的其他输入字段。就像是绑在一起的。很抱歉问了这个愚蠢的问题,我不太懂JavaScript。但不管怎样,我怎么才能解决这个问题??

jQuery(document).ready(function( $) {
var mediaUploader;
$(\'#upload-button-1, #upload-button-2, #upload-button-3\').on(\'click\', function(e) {
e.preventDefault();
if( mediaUploader ){
mediaUploader.open();
return;
}
mediaUploader = wp.media.frames.file_frame = wp.media({
title: \'Upload\', 
button: {
    text: \'Upload\'
},
multiple: false
});

mediaUploader.on(\'select\', function () {
attachment = mediaUploader.state().get(\'selection\').first().toJSON();
$(\'#preview-fav, #preview-grav, #preview-thumb\').val(attachment.url);
$(\'.favicon-preview, .gravatar-preview, .thumbnail-preview\')
.css(\'background\',\'url(\' + attachment.url + \')\');

});

mediaUploader.open();

});
});

2 个回复
最合适的回答,由SO网友:Steven 整理而成

目前的情况是,您正在将选定的媒体项分配给以下行中的所有三个输入:

j(\'#preview-fav, #preview-grav, #preview-thumb\').val(attachment.url);
为了只将值传递给其中一个,需要更改该行以响应按钮中设置的某种标记。在下面的示例中,我设置了data javascript中引用的按钮中的属性。

因此,假设您有以下html:

<!-- upload buttons -->
<input type="button" class="upload-button" data-target="input_1" value="Upload button 1" />
<input type="button" class="upload-button" data-target="input_2" value="Upload button 2" />
<input type="button" class="upload-button" data-target="input_3" value="Upload button 3" />

<!-- inputs to receive the value -->
<input type="text" id="input_1" value="" />
<input type="text" id="input_2" value="" />
<input type="text" id="input_3" value="" />
现在可以在js中执行此操作:

jQuery(function($) {

    $(document).ready(function () {

            var mediaUploader;

            // we can now use a single class name to reference all upload buttons
            $(\'.wp-admin\').on(\'click\', \'.upload-button\', function(e) {

                e.preventDefault();                

                // store the element that was clicked for use later
                trigger = $(this);

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

                mediaUploader = wp.media.frames.file_frame = wp.media({
                    title: \'Upload\', 
                    button: {
                        text: \'Upload\'
                    },
                    multiple: false
                });

                mediaUploader.on(\'select\', function() {

                    attachment = mediaUploader.state().get(\'selection\').first().toJSON();

                    // we\'re replacing this line:
                    //$(\'#preview-fav, #preview-grav, #preview-thumb\').val(attachment.url);

                    // assign the value of attachment to an input based on the data-target value
                    // of the button that was clicked to launch the media browser
                    $(\'#\' + trigger.data(\'target\') ).val(attachment.url);

                    $(\'.favicon-preview, .gravatar-preview, .thumbnail-preview\').css(\'background\',\'url(\' + attachment.url + \')\');
                });

                mediaUploader.open();

            });            

    });

});
更新基于粘贴的代码,您可以通过以下方式提高效率:

function sweetlola_images(){

    $imgs = array(
        \'gravatar\'  => \'img_1\',
        \'thumbnail\' => \'img_2\',
    );

    foreach( $imgs as $name => $id ){

        // get the image
        $url = esc_attr( get_option( $name ) );

        // if there isn\'t one, set $gravatar as empty string
        $url = $url ? $url : \'\';

        //upload button
        echo \'<input type="button" data-target="\' . $id . \'"  value="Upload" class="button button-secondary upload-button" />\';

        //input text field
        echo \'<input type="text" id="\' . $id . \'" name="\' . $name . \'" value="\' . $url . \'" />\';

        if( empty( $url ) ){ //if the value is empty this will show a just the upload button
            echo \'<input type="button" value="Remove" data-remove_target="\' . $id . \'" class="button button-secondary" />\';
        }

        // note
        echo \'<p><i>Idem.<br />Resolução: 89px X 89px</i></p>\'; 

    }
}
将三种图像类型放入$imgs 数组,并使用单个函数生成它们。

SO网友:Lola Bittencourt

实际上,我在php函数中包含html:

function sweetlola_blog_gravatar(){
$gravatar = esc_attr( get_option( \'gravatar_img\' ));
if ( empty ($gravatar) ){ //if the value is empty this will show a just the upload button
echo \'<input type="button" value="Upload" id="upload-button-2" class="button button-secondary" /> //upload buton
<input type="text" id="preview-grav" name="gravatar_img" value="" />
<p><i>Idem.<br />Resolução: 89px X 89px</i></p>\';//input text field
} else { //otherwise it will show an upload and a remove button
echo \'<input type="button" value="Upload" id="upload-button-2" class="button button-secondary" /> //upload button
<input type="text" id="preview-grav" name="gravatar_img" value="\'.$gravatar.\'" /> //input text field
<input type="button" value="Remove" id="remove-button-2" class="button button-secondary" /><p><i>Idem.<br />Resolução: 89px X 89px</i></p>\'; //remove button
}
}

function sweetlola_blog_gravatar(){
$gravatar = esc_attr( get_option( \'gravatar_img\' ));
if ( empty ($gravatar) ){
echo \'<input type="button" value="Upload" id="upload-button-2" class="button button-secondary" />
<input type="text" id="preview-grav" name="gravatar_img" value="" />
<p><i>Idem.<br />Resolução: 89px X 89px</i></p>\';
} else {
echo \'<input type="button" value="Upload" id="upload-button-2" class="button button-secondary" />
<input type="text" id="preview-grav" name="gravatar_img" value="\'.$gravatar.\'" />
<input type="button" value="Remove" id="remove-button-2" class="button button-secondary" />
<p><i>Idem.<br />Resolução: 89px X 89px</i></p>\';
}
}

function sweetlola_blog_thumbnail(){
$thumbnail = esc_attr( get_option( \'thumbnail_img\' ));
if ( empty ($thumbnail) ){
echo \'<input type="button" value="Upload" id="upload-button-3" class="button button-secondary" id="upload-button-3" />
<input type="text" id="preview-thumb" name="thumbnail_img" value="" />
<p><i>Resolução-mínima(imagens menores<br /> ficarão desfocadas): 118px X 118px</i></p>\';
} else {
echo \'<input type="button" value="Upload" id="upload-button-3" class="button button-secondary" id="upload-button-3" />
<input type="text" id="preview-thumb" name="thumbnail_img" value="\'.$thumbnail.\'" />
<input type="button" value="Remove" id="remove-button-3" class="button button-secondary" /><p><i>Resolução-mínima(imagens menores<br /> ficarão desfocadas): 118px X 118px</i></p>\';
}
}

相关推荐

如何使用两个不同文件向jQuery AJAX调用发送php变量

我需要从一个php文件(page.php)向另一个jquery文件(sender.js)中的Ajax调用发送一个私人电子邮件地址。此电子邮件地址必须对用户/或网站不可见,直到用户从wp\\U mail获得自动响应,它必须允许用户答复组织。此电子邮件地址因客户想要联系的组织而异(数百个)。我必须在表单之前从组织获取电子邮件地址,因为它是由javascript生成的。(generated\\u mail.js)发件人。js在页面的标题中被很好地调用。php我在(page.php)中尝试了这段代码——一个组织在