Javascript 如何使用java脚本通过更改用户购物篮中的产品数量来更改总价

Javascript 如何使用java脚本通过更改用户购物篮中的产品数量来更改总价,javascript,jquery,asp.net-core,Javascript,Jquery,Asp.net Core,我想在更改数量时更改总价。 这是一种观点: @foreach (var item in Model) { <tr> <td>@item.Product.Name</td> <td><input type="number" va

我想在更改数量时更改总价。 这是一种观点:

@foreach (var item in Model)
                    {
                        <tr>
                            <td>@item.Product.Name</td>
                            <td><input type="number" value="@item.Count" id="Count" /></td>
                            <td id="Price">@item.Product.Price</td>
                            <td id="SumPrice">@item.SumPrice</td>
                       </tr>
                    }

不能对多个元素使用相同的
id
,而应使用类。然后,使用
$(this.nexist(“tr”)
获取最近的
tr
参考并使用
.find
获取需要计算的
td

演示代码

$('.Count')。在('keyup change',function()上{
//获取最接近的tr->查找价格类获取值
var tot=$(this.recest(“tr”).find('.Price').text()*this.value;
$(this).closest(“tr”).find(“.SumPrice”).text(tot);//设置值
});

Abc
21
21
Xyz
12
12

不能对多个元素使用相同的
ID
,而应使用类。然后,使用
$(this.nexist(“tr”)
获取最近的
tr
参考并使用
.find
获取需要计算的
td

演示代码

$('.Count')。在('keyup change',function()上{
//获取最接近的tr->查找价格类获取值
var tot=$(this.recest(“tr”).find('.Price').text()*this.value;
$(this).closest(“tr”).find(“.SumPrice”).text(tot);//设置值
});

Abc
21
21
Xyz
12
12

当foreach生成多个重复元素时,不能使用固定id(如id=Price,id=SumPrice)。我不知道。我该怎么做才能让它工作呢?当一个foreach产生多个重复元素时,你不能使用固定的id(比如id=Price,id=SumPrice)。我不知道。我能做些什么使它工作呢?谢谢:)它工作了。但是你能告诉我为什么我不能使用相同的ID吗?看看。另外,这篇文章将有助于理解更多:)谢谢:)它奏效了。但是你能告诉我为什么我不能使用相同的ID吗?看看。另外,这篇文章将有助于理解更多:)
 $('#Count').on('keyup',function(){
    var tot = $('#Price').val() * this.value;
    $('#SumPrice').val(tot);
});