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

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

如何從php循環中獲取多個文本框的值并使用javascript對它們求和

如何從php循環中獲取多個文本框的值并使用javascript對它們求和

哈士奇WWW 2021-10-21 14:44:18
我想要的是每一行都應該有它的總和(row1=price x 數量),因為數量變化總和應該自動變化(total=row1 + row2 +row3)所以所有行的總數。因為它只是能夠通過第一行實現這一目標。在這里測試我的代碼 https://malawiclinic.000webhostapp.com/<form class="form-inline">    <?php        $sqlm="SELECT * FROM tbl_wishlist ORDER BY id DESC ";        $resultmn=mysqli_query($db,$sqlm);        $fcount=mysqli_num_rows($resultmn);        if ($fcount>0) {            $countprice=0;            while($found = mysqli_fetch_array($resultmn)) {                $product = $found['product'];                $qty = $found['Quantity'];                $price = $found['price'];                echo "                    <div class='form-group'>                        <label for='exampleInputName2'>$product</label>                        <input type='text' class='form-control' id='price'                               value='$price'>                    </div>                    <div class='form-group'>                        <input type='number' class='input-text form-control'                               id='quantity' value='$qty'>                    </div>                    <label for='exampleInputName2'>$                        <span id='result'></span>                    </label>";            }        } ?></form><script type="text/javascript">    $(document).ready(function(){        $(document).on("input",".input-text", function(){            var x = document.getElementById("quantity").value;            var x1 = document.getElementById("price").value;            var total = x1 * x;            var totals = total.toLocaleString(undefined,                                              {maximumFractionDigits:2});            $("#result").html(totals);            $("#totals").html(totals);        });    });</script>
查看完整描述

1 回答

?
MYYA

TA貢獻1868條經驗 獲得超4個贊

首先,該id屬性為 HTML 元素指定了唯一的 id(該值在 HTML 文檔中必須是唯一的)。您有三個字段 with id="price"、三個 withid="quantity"和三個 withid="result"無效,因此您應該刪除那些id(s)。


現在,您必須使用它們的類名訪問這些字段getElementsByClassName。由于所有字段都form-control作為它們的公共類,下面的代碼將完成這項工作。并將所有內容替換id="result"為class="result".


$(document).ready(function(){

  var input = document.getElementsByClassName('form-control');

  var result = document.getElementsByClassName('result');

  var total = 0;

  for(var i=0; i<input.length; i+=2){

    var product = input[i].value * input[i+1].value;

    total += product;

    product = product.toLocaleString(undefined, {maximumFractionDigits:2});

    result[i/2].innerHTML = product;

  }

  total = total.toLocaleString(undefined, {maximumFractionDigits:2});

  $("#totals").html(total);

});


查看完整回答
反對 回復 2021-10-21
  • 1 回答
  • 0 關注
  • 254 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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