【问题标题】:How to calculate Price and Quantity for dynamically inputs using JQuery如何使用 JQuery 计算动态输入的价格和数量
【发布时间】:2021-12-07 19:10:45
【问题描述】:

我正在使用 PHP、Js 和 MySQL 构建 Ordering food web 应用程序,但无法计算动态输入的价格和数量。

我只能计算价格但没有数量我不知道如何用编码实现,所以如果有人知道解决方案,我将不胜感激。

最后这张图将解释我的理论,以便能很好地理解我的观点: Picture here

这是我的 HTML 代码:

                  <div class="form-group">
                    <label class="control-label col-lg-4"></label>
                      <div id ="diva" class="col-lg-4">
                        <div class="box">
                          <header>
                            <h5>تفاصيل الطلب</h5>
                            </header>
                            <div class="body getval">
                              <div class="form-group">
                              <label class="control-label col-lg-2">الصنف</label>
                                  <div class="col-lg-10">
                                      <select id="itemname" name="itemname[]" class="form-control">
                                        <option disabled selected>اختر الصنف</option>
                                        <?php echo getValues($pdo); ?>
                                      </select>
                                  </div>
                              </div>
                              <div class="form-group">
                                <label class="control-label col-lg-2">السعر</label>
                                <div class="col-lg-10">
                                  <input type="text" id="price" name="price[]" placeholder="-" readonly class="form-control price">
                                </div>
                              </div>
                              <div class="form-group">
                                <label class="control-label col-lg-2">الكمية</label>
                                <div class="col-lg-10">
                                  <input class="form-control quantity" type="number" name="quantity[]" value="0" min="1">
                                </div>
                              </div>
                            </div>
                            </div>
                      </div>
<input type="text" id= "total" placeholder="-" readonly class="form-control">
                    </div>

还有PHP代码:

<?php    
    require 'DBConnection.php';
    $code='';
    if(isset($_POST["code"])){
        $id = $_POST["code"];
        $get_c = $pdo->prepare("SELECT * FROM all_menu WHERE `item_name` = '".$id."'");
        $get_c->execute(); 
        while ($row = $get_c->fetch()) {
            $code .= $row['price'];
        }
        echo $code.'.00';
    }
?>

最后是 JQuery 代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>  
<script>
$(document).ready(function(){
    var nextRowID = 0;
    $('#add').click(function(e){
    e.preventDefault();
    var id = ++nextRowID;
    $('#append').append('<div class="form-group"><label class="control-label col-lg-4"></label><div id ="diva" class="col-lg-4"><div class="box"><header><h5>تفاصيل الطلب</h5></header><div class="body getval"><div class="form-group"><label class="control-label col-lg-2">الصنف</label><div class="col-lg-10"><select id="itemname'+id+'" name="itemname[]" class="form-control"><option disabled selected>اختر الصنف</option><?php echo getValues($pdo); ?></select></div></div><div class="form-group"><label class="control-label col-lg-2">السعر</label><div class="col-lg-10"><input type="text" id="price'+id+'" name="price[]" placeholder="-" readonly class="form-control price"></div></div><div class="form-group"><label class="control-label col-lg-2">الكمية</label><div class="col-lg-10"><input class="form-control quantity" type="number" name="quantity[]" value="0" min="1"></div></div></div></div></div></div>');

      $('#itemname'+id+'').change(function(){
        var code = $(this).val();  
        $.ajax({
          type: 'POST',
          url: 'pages/GetPrice.php',
          data:{code:code},
          success: function(data){
            document.getElementById('price'+id+'').value = data;
          },
          error: function (jqXHR, textStatus, errorThrown){
            alert(errorThrown);
          }
        });
      });
    });

  $('#itemname').change(function(){
        var code = $(this).val();  
        $.ajax({
          type: 'POST',
          url: 'pages/GetPrice.php',
          data:{code:code},
          success: function(data){
            document.getElementById('price').value = data;
          },
          error: function (jqXHR, textStatus, errorThrown){
            alert(errorThrown);
          }
        });
      });
});
</script>

【问题讨论】:

  • 我在您的 HTML 中没有看到 Total 的字段
  • 不不不,我有Total 的字段,但我忘了写在这里:&lt;input type="text" id= "total" placeholder="-" readonly class="form-control"&gt;
  • 好的,我添加了答案

标签: javascript php jquery mysql ajax


【解决方案1】:

好的,那么您需要为每当数量更改时设置一个事件。它将所有价格*数量相加并将结果汇​​总。

function handleQuantityChange(){
  $('.quantity').unbind('change').change(function(){
    let dTot = 0.0;
    $('.price').each(function(){
      dTot += parseFloat($(this).val()) * parseFloat($(this).parent().parent().next().find('.quantity').val())
    });
    $('#total').val(dTot)
  })
}

这是查看我的示例的链接:jsFiddle Example

【讨论】:

  • Yessssssss 非常感谢,我真的很感激。非常感谢^^
猜你喜欢
  • 1970-01-01
  • 2012-09-25
  • 2019-01-24
  • 1970-01-01
  • 1970-01-01
  • 2015-10-29
  • 2013-06-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多