我想为从我的数据库生成的每一行 (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 页面? 谢谢
最佳答案
您正在使用 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);
}
});
})
关于php - 你能为表格中的一行设置一个值吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15047458/