从php对象的jquery数组中设置值?
我通过ajax将产品项的唯一id传递给服务器,从而获得产品项的特定列表。现在,每个产品都有自己的一组属性,我必须在带有产品图像的页面上显示这些属性。当我通过jquery设置值时,只打印数组中的最后一个值。以下是我的编码文件 images.php从php对象的jquery数组中设置值?,php,jquery,arrays,dom,dynamic-html,Php,Jquery,Arrays,Dom,Dynamic Html,我通过ajax将产品项的唯一id传递给服务器,从而获得产品项的特定列表。现在,每个产品都有自己的一组属性,我必须在带有产品图像的页面上显示这些属性。当我通过jquery设置值时,只打印数组中的最后一个值。以下是我的编码文件 images.php while($fetch = mysql_fetch_array($result)) { ?> <div class="col-sm-4"> <div class="thumbn
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
}
您可以看到foreach的代码只覆盖了
$('.productimage'),
$('.productitemname')
// and so on
因此,您只能看到响应的最后数据
$.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);
});
$。每个(数据、函数(){
var getprodId=this.prodId;
var getimageURL=this.imageURL;
var getprice=这个价格;
var getitemName=this.itemName;
var getitemID=this.itemID;
//创建一个标记
变量a=$('');
a、 attr('href','productpurchase.php?id='+getitemID);
//创建新图像
变量img=$('')
prodname.html(getitemName);
var prodprice=$('');
html(getprice);
//将图像插入到
a、 附加(img);
变量容器=$('');
//把它们全部结合起来
容器。附加(a);
container.append(prodname);
容器。附加(产品价格);
//附加到文档
//您可以根据需要更改此项
//完成
$('body')。追加(容器);
});
在这里,我为foreach的每个迭代创建了一个动态dom元素
然后,它将创建一组新的数据,然后插入/包含/追加
添加到html元素的替代解决方案 如果为每个产品块添加某种标识符,如下所示:
请在调用ajax控制台.log(数据)时打印响应;[对象,对象,对象,对象,对象,对象]对象0价格:“800”图像URL:“a1.jpg”项目ID:“55”项目名称:“打印灰色衬衫”类似对象1,2。。。
$.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);
});