你能为表中的一行设置一个值吗


can you set a value for a row in a table?

我想为从数据库生成的每一行(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);
        }
    }); 
})