2013-02-24 40 views
2

我想为我的每个行(tr)从我的数据库生成一个行值。所以我们说,我已经产生如下表:你可以设置表中某一行的值吗?

while ($row = $database->fetch_array($result)) 
{ 
$i=1-$i; 
$class = "row".$i; 


echo "<tr class='{$class} productId' id='{$row['productId']}'> 

<td > ". $row['category']."</td> 
<td >" . $row['productNo']. "</td> 
<td>" . $row['productName']. "</td> 
<td class='edit'>" . intval($row['quantity']). "</td> 
<td>" . $row['sfLf']. "</td> 
<td>". $row['cost']. "</td> 
<td>". $row['total']."</td> 
    </tr>"; 
} 

在这里,我试图通过id属性来传递的ProductID值,但不幸的是,ID停留时,我在下面的脚本尝试检索所有行相同:

$(".productId").click(function() { 
    $.ajax({ 
     type: "POST", 
     url: "populateInventory.php", 
     data: "productId="+$(".productId").attr('id'), 
     success: function(msg){ 
      $("#invHistoryTable").html(msg); 
     } 
    }); 

如何使用上述ajax命令将正确的productId值传递给我的php页面? 谢谢

+0

即便我做了以下我现在还无法得到正确的结果:数据:{产品编号:$( “productId参数”)ATTR( '身份证')} – RazorHead 2013-02-24 00:52:16

回答

1

您正在使用post方法,但您要发送一个字符串。您应该发送一个键/值对,也就是attr返回jQuery集合中第一个选定元素的ID,而不是触发事件的元素,您可以使用引用点击元素的this关键字。

$(".productId").click(function(event) { 
    // event.preventDefault(); 
    $.ajax({ 
     type: "POST", 
     url: "populateInventory.php", 
     data: { productId: this.id } , 
     success: function(msg){ 
      $("#invHistoryTable").html(msg); 
     } 
    }); 
}) 
+0

真棒。它做到了。非常感谢,你是一位人生的救星 – RazorHead 2013-02-24 00:54:43

相关问题