如何根据HTML下拉列表中的数量选择来计算价格

How to calculate the price according to the quantity selection in dropdown in HTML

本文关键字:选择 计算 何根 HTML 下拉列表      更新时间:2023-09-26

嗨,我正在做一个类似购物车的应用程序。在我的下拉列表中,用户应该根据价格的变化来选择数量。如何实现这一点?听说我们可以用javascript中的onChange()函数来实现。帮助我实现这个

<p id="price"> <h3> MRP - <?php echo $row['price'] ?> </h3> </p>
<p> Total Price: Total price should load here </p>

    <select class="form-control" name='item' id='item' onchange=''>
    <option>Select Quantity</option>
    <option>1</option>
    <option>2</option>
    <option>3</option>
    <option>4</option>
    <option>5</option>
    </select>

您可以在这里查看演示此处演示

function getamount(value)
 {
 var price=$("#price").html().replace ( /[^'d.]/g, '' );
 var amount=parseInt(value)*parseInt(price);
 alert(amount);
 $("#amount").html("Total Price: " +amount);
 }

如果您想在php中获得价格,请执行:

<form method="post">
  <select class="form-control" name='item' id='item' onchange=''>
  <option>Select Quantity</option>
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  </select>
</form>
<?php 
$_POST['item']; //gives you the value that is selected when form is submitted 
?>

还有第二种方法可以获取onChange的值,然后使用javascript获取值。但是,只有当用户更改值时,您想直接执行某些操作时,这才是必要的。然后,如果你想直接用php做一些有值的事情,你需要做一个ajax请求。