dscqrkvr9562034621 2018-09-07 11:35
浏览 76
已采纳

Php id在js中识别

I have a loop in php and I'm using a js function onclick to get the id from the php loop. I do loop in js to recognize a php dynamic id, but it recognizes the wrong order of the loop item.

Sample php code:

$list = 1;        
for ($i = 0; $i < 7; $i++ ){
    $listNo = $list;
    $list++;
    $html .= "<div class='row l'>";
    $html .= " <div class='col-xl-3 col-lg-8 col-6'>
            <input name=']' id='dm_slider_img_url_$listNo' type='text' value='" . $dm_slides[++$counter] . "'/>
            <button class='set_custom_images' id='meta-image-button-$listNo'>
                <span class='dashicons dashicons-plus-alt'></span>
            </button>
        </div>
        <div class='col-xl-1 col-lg-4 col-6 mr-auto'>
            <img id='dm_slider_img_preview_$listNo' src='" . $dm_slides[$counter++] . "' name=''>
        </div>";
    $html .= "</div>";
}

Sample js code:

var send_attachment_bkp = wp.media.editor.send.attachment;
for (let ids=1; ids<8; ids++) {
    if ($('#meta-image-button-'+ids).length > 0) {
        if ( typeof wp !== 'undefined' && wp.media && wp.media.editor) {
            $(document).on('click', '.set_custom_images', function(e) {
                e.preventDefault();
                var button = $(this);
                var id = button.prev();
                wp.media.editor.send.attachment = function(props, attachment) {
                    $('#dm_slider_img_url_'+ids).val(attachment.url);
                    $('#dm_slider_img_preview_'+ids).attr('src',attachment.url);
                    wp.media.editor.send.attachment = send_attachment_bkp;
                };
                wp.media.editor.open(button);
                return false;
            });
        }
    }
}

What am I doing wrong?

  • 写回答

1条回答 默认 最新

  • dongrouyuan5685 2018-09-07 14:23
    关注

    After looking other questions related to getting id, I figured it out. After calling click function using button class, I stored it as variable and added this variable.

        $(document).ready(function() {
        var $ = jQuery;
        // var ids = 1;
        // $(«.set_custom_images»).onclick = function(t) {t.attr(«id»)}
        var send_attachment_bkp = wp.media.editor.send.attachment;
            if ($("[id^='meta-image-button-']" ).length > 0) {
                if ( typeof wp !== 'undefined' && wp.media && wp.media.editor) {
                    $(document).on('click', '.set_custom_images', function(e) {
                        var clickedID = this.id;
                        e.preventDefault();
                        var button = $(this);
                        var id = button.prev();
                        wp.media.editor.send.attachment = function(props, attachment) {
                                $('#dm_slider_img_url_'+clickedID).val(attachment.url);
                       $('#dm_slider_img_preview_'+clickedID).attr('src',attachment.url);
                                wp.media.editor.send.attachment = send_attachment_bkp;
                        };
                        wp.media.editor.open(button);
                        return false;
                    });
                }
            }
    });
    

    And in php make some changes

    $list = 1;        
    for ($i = 0; $i < 7; $i++ ){
        $listNo = $list;
        $list++;
        $html .= "<div class='row l'>";
        $html .= " <div class='col-xl-3 col-lg-8 col-6'>
                <input name='' id='dm_slider_img_url_m_$listNo' type='text' value='" . $dm_slides[++$counter] . "'/>
                <button class='set_custom_images' id='m_$listNo'>
                    <span class='dashicons dashicons-plus-alt'></span>
                </button>
            </div>
            <div class='col-xl-1 col-lg-4 col-6 mr-auto'>
                <img id='dm_slider_img_preview_m_$listNo' src='" . $dm_slides[$counter++] . "' name=''>
            </div>";
        $html .= "</div>";
    }
    

    I hope this will help for someone who searches solution like this

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来