gpt4 book ai didi

javascript - 带总和的 jQuery 选择器

转载 作者:搜寻专家 更新时间:2023-10-31 22:03:23 25 4
gpt4 key购买 nike

我不知道我的问题的合适标题,这就是我没有搜索的原因。

我的问题如下:

我有一段HTML代码,例如:

<input type="text" id="product_qty_1" value="12" />
<input type="text" id="product_qty_2" value="21" />
<input type="text" id="product_qty_3" value="45" />

我想通过 jQuery 对这些值求和,但这需要自动进行,因为我们不知道那里有多少个输入

我不知道如何处理选择器 input[id*="product_qty_"] 才能使其正常工作,并对值求和!

提前致谢!

最佳答案

var total=0;

$("input[type='text']").each(function() {
total += parseInt(this.value, 10);
});

关于javascript - 带总和的 jQuery 选择器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11658797/

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