简体   繁体   English

jQuery:如何获取多个img的src值

[英]jQuery : How To Get src Value of Multiple img

I have this PHP script to generate thumbnails of a directory. 我有这个PHP脚本来生成目录的缩略图。 number of files inside that directory are dynamic : 该目录中的文件数量是动态的:

<?
    if (isset ($product_imgfiles)) {
        echo '<div class="row">';

        for ($i=2; $i<($num_product_imgfiles+2); $i++) {
            echo '<div class="col-xs-4 col-md-2" style="padding-left: 7px; padding-right: 7px;">
                    <a href="#" class="thumbnail product_imgfiles">
                        <img src="'. $directory . $product_imgfiles[$i] .'" style="margin-bottom: 5px;">
                        <span><i class="fa fa-times-circle"></i> hapus</span>
                    </a>
                </div>';
        }

        echo '</div>';
    }
?>

and I also have this jquery to listen the event each time the thumbnail is clicked : 而且我也有这个jquery每次单击缩略图时都监听事件:

echo '
$( ".product_imgfiles" ).each( function(index)  {
    $( ".product_imgfiles" ).click(function() {
        event.preventDefault();
        var img_files = $( ".product_imgfiles img" ).attr( "src" );
        alert( img_files );
        alert( \''.$product_token.'\' );
        alert( '.$secret_token.' );
    });
}); 

';

the jquery part is able to show alert message. jQuery部分能够显示警报消息。 but, unfortunately, the loop won't stop until number of product_imgfiles and it shows same src value of img tag. 但是,不幸的是,直到product_imgfiles数量增加并且循环显示img标签的src值相同,循环才会停止。

while what I need is to get src value of img tag once the class product_imgfiles is clicked. 而我需要的是单击class product_imgfiles类后获取img标签的src值。 how to get each src value of each img class when it's clicked? 单击时如何获取每个img类的每个src值? thank you. 谢谢。

Remove the .each loop. 删除.each循环。

.click() inherently applies it to each element already, you don't need to loop over them again. .click()已将其固有地应用于每个元素,您无需再次遍历它们。

echo '
    $( ".product_imgfiles" ).click(function(event) {
        event.preventDefault();
        var img_files = $( this ).children('img').attr( "src" );
        alert( img_files );
        alert( \''.$product_token.'\' );
        alert( '.$secret_token.' );
    });
';

Additionally, you need to actually pass the event variable into the constructor. 此外,您实际上需要将event变量传递到构造函数中。 As was brought out in the comments, you reference the current object being clicked with this instead of using the selector again. 正如在评论中提出来的,您引用被点击与当前对象this ,而不是再次使用选择。

Also, that confusing bit with the product token and secret token is a little strange. 而且,将产品令牌和秘密令牌与混淆位还有些奇怪。 It's going to be the same for every .product_imgfiles object. 每个.product_imgfiles对象都将相同。 Is that what you want? 那是你要的吗? If so, that's good! 如果是这样,那就太好了! If not, you should store those values as an attribute for each img element in PHP (eg data-product_token='$product_token' ) and then refer to them in JS with something like $(this).attr('data-product_token') or $(this).data('product_token') . 如果不是,则应将这些值存储为PHP中每个img元素的属性(例如, data-product_token='$product_token' ),然后在JS中使用$(this).attr('data-product_token')$(this).data('product_token')

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

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