簡體   English   中英

嘗試回顯data-id值

[英]Trying to echo the data-id values

我試圖從鏈接中獲取data-id,然后打開一個模式,在該模式下,我想使用php函數中data-id中保存的數據。 但是當我回聲時,我什么也得不到。

我無法弄清楚哪里出了問題,正在尋找一些建議。

謝謝。

我有以下data-id鏈接。

    <div class="package-list-item">
        <a href="javascript:;" class="group-link" data-id="1235"> 
            <figure>
                <img src="<?php echo get_the_post_thumbnail_url( 12121 , 'thumbnail'); ?>" class="img-responsive" alt="<?php echo get_the_title( 12121 ); ?>"><br>
                <figcaption><?php echo get_the_title( 12121 ); ?></figcaption>
            </figure>
        </a>
    </div>

以及以下在我的模態中-channel.php

if(isset($_POST['package_id']) && !empty($_POST['package_id'])) { 
  echo $package_id;
};

和以下JavaScript

$(document).on('click', '.group-link', function() {
                var package_button = $(this),
                    package_id = package_button.data('id'),
                    token = "STFXQAJ0yYLdjiYAI35hrcEZzXRplq3tQRx7EgD0",
                    channels_window = $('#channels_window'),
                    body = $('body');

                channels_window.load("channel.php", { package:package_id, _token:token }, function() {
                    channels_window.fadeIn('slow');
                    body.addClass('window-opened');
                });
            });

按照您的代碼,您應該獲取package而不是package_id因為它是您提供的參數的名稱。

$package_id = $_POST['package'];

if($package_id) { 

 echo $package_id;

};

使用.attr函數獲取數據屬性值

$(document).on('click', '.group-link', function() {
                var package_button = $(this),
                    package_id = package_button.attr('data-id'),
                    token = "STFXQAJ0yYLdjiYAI35hrcEZzXRplq3tQRx7EgD0",
                    channels_window = $('#channels_window'),
                    body = $('body');

                channels_window.load("channel.php", { package:package_id, _token:token }, function() {
                    channels_window.fadeIn('slow');
                    body.addClass('window-opened');
                });
            });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM