我有一个小问题,基本上我有一个隐藏的输入按钮,它加载了数据库表中的唯一值:<input type="hidden" name="ProductId" class="ProductId" value='.$row["ProductId"].' />
根据通过此方法返回的行数,此按钮会重复:
while ($row = $result->fetch()) {
echo '<table class="cart-row" cellspacing="0" cellpadding="0" width="100%">';
echo '<tbody>';
echo '<tr>';
echo '<td width="75"><img border="0" width="59px" height="78px" title="" alt="" src=' . $row["ImagePath"] .'></td>';
echo '<td width="203"><span class="itemElements itemName">'. $row["Name"] .'</span></td>';
echo '<td width="203"><input type="submit" class="btnMinus linkbtnType2" value="-"><input type="submit" class="btnPlus linkbtnType2" value="+"></td>';
echo '<td width="135"><span class="qtyNum">('. $row["Qty"] .')</span> <br />';
echo '<input type="hidden" name="ProductId" class="ProductId" value='.$row["ProductId"].' />';
echo '<span class="qtyRemoveLink"><input type="submit" class="btnRemove linkbtn" value="Remove"></td>';
echo '<td width="180"><span class="itemElements orderStatus">In Stock Usually dispatched within 24 hours</span></td>';
echo '<td width="175" class="itemPriceRow"><span id="itemPrice">€ '. $row["Price"] .'</span></td>';
echo '</tr>';
echo '</tbody>';
echo '</table>';
echo '<br>';
}
我使用 jQuery 方法从隐藏按钮读取此值,但它仅从生成的第一个输入按钮读取值。我尝试将按钮从 ID 更改为类,但没有成功。
这是 jQuery 方法:
$('.btnRemove').click(function() {
var productId = $(".ProductId").val();
$.ajax({
type: "POST",
url: "functions/deleteCartItem.php",
data: "productId="+productId+ "",
success: function(msg){
alert(msg);
}
})
})
我能想到的一个可能的解决方案是添加一个唯一的 id
到每个按钮,这样它们不仅可以通过名称来识别,还可以通过 id
来识别。 。然而,这在从 jQuery 方法读取它时带来了问题。
有什么想法吗?
最佳答案
尝试:
var productId = $(this).closest('tr').find(".ProductId").val();
this
引用 $('.btnRemove')
作为 DOM 元素。
jQuery(this).closest("tr")
我们正在 DOM 树中向上搜索第一个 tr
元素。
从这里我们搜索.ProductId
。
安德烈亚斯
关于jquery - 为 <input> 按钮分配唯一的 id 并使用 jquery 读取它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8601670/