简体   繁体   English

我的图片来源(src)是$ file_path php回显,如何将单击的$ file_path传递给javascript?

[英]My Image sources (src) are a $file_path php echo, how I can pass the clicked $file_path to javascript?

So I have the below code to put files from folder as a gallery images, using loop 所以我有以下代码使用循环将文件夹中的文件作为图库图像放置

<?php
        $path = "server/php/files/"; // path to images folder
        $file_count = count(glob($path . "*.{png,jpg,jpeg,gif}", GLOB_BRACE));
        if($file_count > 0)
        {
            $fp = opendir($path);
            while($file = readdir($fp))
            {
                $ext = pathinfo($file, PATHINFO_EXTENSION);
                $ext_array = ['png', 'jpg', 'jpeg', 'gif'];
                if (in_array($ext, $ext_array))
                {
                    $file_path = $path . $file;?>
                    <div class="col-md-4 col-xs-6">
                        <button href="" id="img1" style="background-color: #ffffff; border: none;color: white;padding-left:3.0rem;text-align: center;text-decoration: none;display: inline-block;font-size: 16px; float:left;" onclick="myFunction(this.src)" title="My Favorites" data-gallery><img class="floating-box" src="<?php echo $file_path; ?>" class="img-responsive" /></button>
                    </div>
                <?}
            }
            closedir($fp);
        }
        else
        {
            echo "Sorry! There are no images in the gallery!!!";
        }
        ?>

The onclick="myFunction(this.src) should pass the src parameter to the onclick function, but the result I get is undefined onclick =“ myFunction(this.src)应该将src参数传递给onclick函数,但是我得到的结果是不确定的

<script>
        function myFunction(element) {

            $.ajax({
            type: "POST",
            cache: false,
            url: "data_change.php",
            data: {value: element}, //send a new value to the "my_backend.php" script
            beforeSend: function (html) {
                //show a preloader here
            },
            success: function (html) {
                //do something if request is successfully completed
                alert(element);
            }

        })


        };




        </script>

This passes the button element, not the image element inside it, where you have your src. This会将按钮元素而不是其中的图像元素传递给您的src。

You can do something like this to get what you need: 您可以执行以下操作来获得所需的内容:

data: {value: element.children[0].src},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM