php - Metabox Wordpress 插件 Thickbox jQuery 错误 - Uncaught TypeError : Cannot read property 'replace' of undefined

标签 php jquery wordpress

我在 Wordpress 中使用带有 thickbox 选项的 Metabox 插件。

除了尝试使用多张图片上传创建/编辑图库时,一切正常。

当我点击“插入图库”按钮时,没有任何反应,我在 chrome 控制台中注意到:

Uncaught TypeError: Cannot read property 'replace' of undefined

文件内容如下

jQuery( function ( $ )
{
    'use strict';

    $( 'body' ).on( 'click', '.rwmb-thickbox-upload', function ()
    {
        var $this = $( this ),
            $holder = $this.siblings( '.rwmb-images' ),
            post_id = $( '#post_ID' ).val(),
            field_id = $this.data( 'field_id' ),
            backup = window.send_to_editor;

        window.send_to_editor = function ( html )
        {
            var $img = $( '<div />' ).append( html ).find( 'img' ),
                url = $img.attr( 'src' ),
                img_class = $img.attr( 'class' ),
                id = parseInt( img_class.replace( /\D/g, '' ), 10 );

            html = '<li id="item_' + id + '">';
            html += '<img src="' + url + '">';
            html += '<div class="rwmb-image-bar">';
            html += '<a class="rwmb-delete-file" href="#" data-attachment_id="' + id + '">×</a>';
            html += '</div>';
            html += '<input type="hidden" name="' + field_id + '[]" value="' + id + '">';
            html += '</li>';

            $holder.append( $( html ) ).removeClass( 'hidden' );

            tb_remove();
            window.send_to_editor = backup;
        };
        tb_show( '', 'media-upload.php?post_id=' + post_id + '&TB_iframe=true' );

        return false;
    } );
} );

这是给出错误的行

id = parseInt( img_class.replace( /\D/g, '' ), 10 );

最佳答案

发生此错误是因为 html 没有 img 标签... 将您的代码更改为:

var $img = $( '<div />' ).append( html ).find( 'i' );
url = $img.attr( 'src' ),
img_class = $img.attr( 'class' ),
id = parseInt( img_class.replace( /\D/g, '' ), 10 );

关于php - Metabox Wordpress 插件 Thickbox jQuery 错误 - Uncaught TypeError : Cannot read property 'replace' of undefined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35520245/

相关文章:

php - 检测 Str 中的透明度值

php - 显示没有产品详细信息的空白格式页面

php - 在 PHP 中,我如何知道哪些值与数组中的特定值相邻?

javascript - 如何在用户选择特定变体时为弹出窗口添加链接

php - 用户和公司 - 数据库架构

javascript - JavaScript 字符串中的不间断空格是如何表示的?

javascript - 从给定数据生成 json 对象

javascript - 两个选择表列

css - 使用我的 CSS 定位 Woocommerce 订单接收页面

wordpress - 调用未定义的函数 add_options_page()