首页 > 解决方案 > 如何在使用循环从下拉列表中选择一个选项时从数据库中输出一个值?

问题描述

我是 laravel 的新手。

我有一个来自数据库的数据集,它存储在变量 product_data 中。

姓名 number_of_products 价格
0-50k 2 200
51-100k 3 123
100-150k 6 345
150-200k 8 450

我想将名称写为下拉列表,在选择特定名称时,产品数量和价格应显示为文本。我希望为每个名称添加另一个数量下拉列表,数量应该是产品数量的最大值。

例如:如果我从下拉列表中选择名称 51-100k,价格 123 应显示在文本字段中,产品数量 3 应显示在文本字段中,下拉列表中的数量应在 1 到 3 范围内。此字段还应保存到数据库中的另一个表,表名为 bid_price,字段为 name、number_of_products、bid_price、original_Price。

我尝试过的是:

<div class="form-group row">    
     <label>Select Milege gap: </label>
<select class="form-select" name="mileage" id="mileage" onchange="getOption()">
    {% for p in product_data %}
    <option value="{{p.name}}">{{p.name}}</option>
    {% endfor %}
</select>           
</div>
 <div class="form-group row">   
     <label>Avalaible Quantity: </label>
        <input type="text" class="form-control" readonly name="avaialbe_qty"/>
        
</div>
  <div class="form-group row">  
     <label>Quantity: </label>
<select class="form-select" name="quantity" id="quantity" >
    {% for p in product_data %}
    <option value="{{p.number_of_products}}">{{p.number_of_products}}</option>
    {% endfor %}
</select>           
</div>                                  

  <div class="form-group row">
    <label for="inputBid" class="col-sm-2 col-form-label" >Enter Bid Price</label>
    <div class="col-sm-10">
      <input type="number" class="form-control" id="inputBid" name="bid"  />
    </div>
  </div>
            
 <script>
function getOption()
    {
        var select = document.getElementById('mileage');
        var option = select.options[select.selectedIndex];
        document.getElementById('truck').value = option.value;
    }
</script>
                            
                        

如何根据下拉列表的选择值显示相应的价格和产品数量。?

标签: phplaraveldatabaseloopsdrop-down-menu

解决方案


{% for p in product_data %}
  <option value="{{p.number_of_products}}">{{p.number_of_products}}</option>
    {% endfor %}

这是 Jinja 不是为了使用刀片的刀片

@foreach($product_data as $product)
  <option value="{{$product->number_of_products}}">{{$product->number_of_products}} - ${{$product->price}}</option>
@endforeach

推荐阅读