从jquery数组对象php设置值?

Ami*_*hal 5 php arrays jquery dom dynamic-html

我通过ajax获取特定的产品项列表,方法是将它们的唯一ID传递给服务器.现在每个产品都有自己的一组属性,我必须在页面上显示产品图像.当我通过jquery设置值时,只打印了数组中的最后一个值.以下是我的编码文件.

images.php

while($fetch = mysql_fetch_array($result))
      {
      ?>

      <div class="col-sm-4">
       <div class="thumbnail">

        <a class="productitemid" href="productpurchase.php?id=<?php echo $fetch['itemID'];?>"><img class="img-responsive productimage" src="uploadedfiles\<?php echo $fetch['imageURL'];?>" alt="<?php echo $fetch['imageURL'];?>" /></a>

        <div class="text-center productitemname" style="font-weight:bold;"><?php echo $fetch['itemName']; ?></div>
        <div class="badge col-sm-offset-1 productprice"><?php echo $fetch['price']; ?></div>
        <span class="col-md-offset-7"><a class="productitemid btn btn-success" href="productpurchase.php?id=<?php echo $fetch['itemID'];?>">BUY</a></span>

       </div>
      </div>
      <?php
      }
Run Code Online (Sandbox Code Playgroud)

js文件

$(document).ready(function(){
  $('.menProdCatgry').on('click',function(){
   $.ajax({
    type: "post",
    url: "getselectedproducts.php",
    data:{
     "prodId" : $('.menProdCatgry').attr('prodCatId')
    },
    dataType: "json",
    success: function(data){
     console.log(data);
     $.each(data, function(){
     var getprodId = this.prodId;
     var getimageURL = this.imageURL;
     var getprice = this.price;
     var getitemName = this.itemName;
     var getitemID = this.itemID;

     $('.productimage').attr('src','uploadedfiles\/'+getimageURL);
     $('.productitemname').text(getitemName);
     $('.productprice').text(getprice);
     $('.productitemid').attr('href','productpurchase.php?id='+getitemID);

      });

    },
    error: function(data){
     console.log(data);
    }

   });
  });
 });
Run Code Online (Sandbox Code Playgroud)

Oli*_* B. 1

您可以看到 foreach 的代码仅覆盖了 the 的值和属性

 $('.productimage'),
 $('.productitemname') 
 // and so on
Run Code Online (Sandbox Code Playgroud)

所以你只能看到响应的最后数据

$.each(data, function() {
            var getprodId = this.prodId;
            var getimageURL = this.imageURL;
            var getprice = this.price;
            var getitemName = this.itemName;
            var getitemID = this.itemID;

            // create a tag
            var a = $('<a/>');
                a.attr('href', 'productpurchase.php?id='+getitemID);
            // create new image
            var img = $('<img/>');
                img.attr('src', 'uploadedfiles/'+getimageURL);

            var prodname = $('<div/>')
                prodname.html(getitemName);

            var prodprice = $('<div/>');
                prodprice.html(getprice);
                // insert image to a
                a.append(img);

            var container = $('<div/>');
            // combine them all
            container.append(a);
            container.append(prodname);
            container.append(prodprice);
            // append to document
            // you can change this according to you need
            // to accomplish
            $('body').append(container);

        });
Run Code Online (Sandbox Code Playgroud)

在这里,我为 foreach 的每次迭代创建了一个动态 dom 元素,然后它将创建一组新的数据,然后它将插入/包含/附加到 html 元素