我的图像源(src)是$ file_path php echo,我如何将点击的$ file_path传递给javascript?

时间:2017-05-29 23:59:52

标签: javascript php jquery

所以我使用以下代码将文件夹中的文件作为图库图像,使用循环

<?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!!!";
        }
        ?>

onclick =&#34; 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>

1 个答案:

答案 0 :(得分:0)

This传递按钮元素,而不是其中包含src的图像元素。

你可以做这样的事情来获得你需要的东西:

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