亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 java 腳本通過更改用戶購物籃中的產品數量來更改總價

如何使用 java 腳本通過更改用戶購物籃中的產品數量來更改總價

當年話下 2023-07-20 14:34:04
我想在更改數量時更改總價。這是視圖:@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>                    }這是我使用的java腳本,但它不起作用。當我更改數量時,總價不會更改。 $('#Count').on('keyup',function(){    var tot = $('#Price').val() * this.value;    $('#SumPrice').val(tot);});
查看完整描述

1 回答

?
搖曳的薔薇

TA貢獻1793條經驗 獲得超6個贊

您不能對ids多個元素使用相同的內容,而是使用類。然后,使用$(this).closest("tr")獲取最接近的tr參考并使用來獲取您需要計算.find的值。td


演示代碼:


$('.Count').on('keyup change', function() {

  //get closest tr -> find price class get value

  var tot = $(this).closest("tr").find('.Price').text() * this.value;

  $(this).closest("tr").find('.SumPrice').text(tot); //set value

});

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>

  <tr>

    <td>Abc</td>

    <!--added class-->

    <td><input type="number" value="1" class="Count" /></td>

    <td class="Price">21</td>

    <td class="SumPrice">21</td>

  </tr>

  <tr>

    <td>Xyz</td>

    <td><input type="number" value="1" class="Count" /></td>

    <td class="Price">12</td>

    <td class="SumPrice">12</td>

  </tr>

</table>


查看完整回答
反對 回復 2023-07-20
  • 1 回答
  • 0 關注
  • 121 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號