gpt4 book ai didi

javascript - 如何获取最后一个元素的id?

转载 作者:行者123 更新时间:2023-11-30 17:29:53 25 4
gpt4 key购买 nike

我有产品 list ,使用下面的代码

<table id="product">
<thead>
<tr>
<th style="width:30%;">Item Name</th>
<th style="width:11%;">Price</th>
<th style="width:11%;">Qty</th>
</tr>
</thead>
<tbody id="cart_contents">
<?php $i=1;
foreach($cart as $line=>$item)
{
?>
<tr>
<td style="align:center;"><?php echo $item['name']; ?></td>
<td><input type="text" id="price_<?php echo $i;?>" value="<?php echo $item['price']; ?>" name="price"></td>
<td id="qnty"><input type="text" id="quantity_<?php echo $i;?>" value="<?php echo $item['quantity']; ?>" name="quantity"></td>
</tr>
<?php
$i++;
}
?>
</tbody>
</table>

我需要编辑最后添加的产品的数量和价格字段。

<table width="297px" style="float:left" class="CSSTableGenerator">
<tr>
<td onclick="click_quantity();"><a href="javascript:void(0);">Qty</a></td>
<td onclick="click_price();"> <a href="javascript:void(0);" >Price</a></td>
</tr>
</table>
function click_quantity(){   
$("#table register").find("td qnty").each(function(){
var id = $(this).attr("id");
alert(id);
})
}

如何在单击Qty 链接后​​获取最后一个数量字段的 id?

我已经尝试了一些代码,但我无法获得最后一个元素的 id。

例如:id 名称是 quantity_1、quantity_2、quantity_3、quantity_4

如何获取quantity_4(最后一个元素的id)?

最佳答案

HTML

<table id="product">
<thead>
<tr>
<th style="width:30%;">Item Name</th>
<th style="width:11%;">Price</th>
<th style="width:11%;">Qty</th>
</tr>
</thead>
<tbody id="cart_contents">
<?php $i=1;
foreach($cart as $line=>$item)
{
?>
<tr>
<td style="align:center;"><?php echo $item['name']; ?></td>
<td>
<input type="text" id="price_<?php echo $i;?>" value="<?php echo $item['price']; ?>" name="price"/>
</td>
<td class="qnty">
<input class="qnty_input" type="text" id="quantity_<?php echo $i;?>" value="<?php echo $item['quantity']; ?>" name="quantity"/>
</td>
</tr>
<?php
$i++;
} ?>
</tbody>
</table>

JS代码

  function click_quantity(){
var id = $("#cart_contents tr:last-child .qnty_input").attr("id");
// rest of your logic
}

关于javascript - 如何获取最后一个元素的id?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23356818/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com