编程语言
首页 > 编程语言> > javascript – 如何从WordPress Thickbox中传递一个值?

javascript – 如何从WordPress Thickbox中传递一个值?

作者:互联网

我目前正在开发一个自定义Wordpress插件,它要求用户在表单中创建一个列表,因此为了帮助他们填充列表,我已经实现了Wordpress Thickbox.我已经使用我想要的内容制作了厚箱显示,但是我正在努力做的是将数据传回原始形式.

原始表格是这样的:

<input name="input_that_wants_data" id="input_for_data" type="text" />
<a href="#TB_inline?width=600&height=550&inlineId=my-content-id" class="thickbox">Click Here for Modal</a>

就像你期望任何形式一样.基本上我希望模态中的信息将我的字符串传递回input_for_data

模态中的代码有多个表行,如下所示:

<td><input type="checkbox" class="modal_checkbox_class" value="'.$data->value.'"></td>

基本上我想要做的是构建每个单击复选框的值的数组,然后使用Javascript的split函数将其转换为字符串,我将返回到模态之外的输入字段.

非常感谢任何和所有的帮助.我更喜欢Javascript / JQuery解决方案

解决方法:

我用这个教程做了你想做的事情:
https://code.tutsplus.com/tutorials/getting-started-with-the-wordpress-media-uploader–cms-22011

我的代码看起来像这样:

    function renderMediaUploader() {
    'use strict';

    var file_frame, image_data;

    /**
     * If an instance of file_frame already exists, then we can open it
     * rather than creating a new instance.
     */
    if ( undefined !== file_frame ) {

        file_frame.open();
        return;

    }

    /**
     * If we're this far, then an instance does not exist, so we need to
     * create our own.
     *
     * Here, use the wp.media library to define the settings of the Media
     * Uploader. We're opting to use the 'post' frame which is a template
     * defined in WordPress core and are initializing the file frame
     * with the 'insert' state.
     *
     * We're also not allowing the user to select more than one image.
     */
    file_frame = wp.media.frames.file_frame = wp.media({
         title: 'Select or Upload Media Of Your Chosen Persuasion',
          button: {
            text: 'Use this media'
          },
        multiple: true
    });

    //add items from thickbox to table
    file_frame.on( 'select', function() {

        var attachment = file_frame.state().get('selection').toJSON();

        jQuery.each(attachment, function(i, val){
            jQuery('table').show();
            jQuery('table tbody').append('<tr class="table_row"><td class="col-sm-2"><img class="img-responsive" src="'+val.url+'"></td><td class="col-sm-8"><input style=" display: block;" type="text" name="entry[url][]" value="'+ val.url +'"></td></tr>');
        });


    });

    // Now display the actual file_frame
    file_frame.open();

}

(function( $) {
    'use strict';

    $(function() {
        $( '#set-footer-thumbnail' ).on( 'click', function( evt ) {

            // Stop the anchor's default behavior
            evt.preventDefault();

            // Display the media uploader
            renderMediaUploader();

        });

    });

})( jQuery );

标签:jquery,wordpress,javascript,thickbox
来源: https://codeday.me/bug/20190701/1352084.html