gpt4 book ai didi

javascript - 在 mutli div 中选择多个输入

转载 作者:行者123 更新时间:2023-12-03 12:40:54 24 4
gpt4 key购买 nike

我正在为产品零售构建 CRM 应用程序

在收据形式中,可能包含同一类别的多种产品

这里是一个类别的示例

<div id="frame_item">
<label>شنبر</label>
<input type="text" id="frame_code" name="frame_code" onfocus="autoSelectFrame(&quot;frame_code&quot;)">
<label>بيانات الشنبر</label>
<input type="text" id="frame_details" name="frame_details" disabled>
<label>سعر الشنبر</label>
<input type="text" id="frame_sell_price" name="frame_sell_price" value="5" class="frame_sell_price" disabled>
<label>الكمية</label>
<input type="text" id="frame_quantity" name="frame_quantity" class="frame_quantity" value="1" onchange="updateItemPrice(&quot;frame&quot;);">
<input type="text" id="frame_total_price" name="frame_total_price" class="frame_total_price" value="" disabled>

假设数量发生变化,它会改变商品的总价

这是我为它制作的自定义函数的 jquery 代码

function updateItemPrice(x)
{
var finalPrice = 0;
$(".frame_item").each(function(i){
var quantity = $(".frame_quantity").val();
var price = $(".frame_sell_price").val();
finalPrice = parseInt(quantity) * parseInt(price);
//$(".frame_total_price").val(finalPrice);
alert(finalPrice);
});
}

它用相同的值更新所有问题

最佳答案

问题是 $(".frame_quantity")$(".frame_sell_price")从文档根搜索,他们找到所有匹配的元素。然后val()检索第一个匹配元素的值。

您需要的是:

var finalPrice = 0;
$(".frame_item").each(function(i){
var item = $(this);
var quantity = item.find(".frame_quantity").val();
var price = item.find(".frame_sell_price").val();
finalPrice = parseInt(quantity) * parseInt(price);
item.find(".frame_total_price").val(finalPrice);
alert(finalPrice);
});

这将触发搜索当前商品内的数量和价格。

同样来自 jQuery 选择器,我假设 <div id="frame_item">实际上是<div class="frame_item">

关于javascript - 在 mutli div 中选择多个输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23529953/

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