php - 如何在使用循环从下拉列表中选择一个选项时从数据库中输出一个值?
问题描述
我是 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>
如何根据下拉列表的选择值显示相应的价格和产品数量。?
解决方案
{% 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
推荐阅读
- c# - 如何更改我的代码以使用后台工作人员而不是后台线程
- f# - 如何反射 MakeUnion 三级类型?
- python - 如何创建一个可变时间函数,我可以在其中选择 python 中的时间(即不是实际的当前时间)?
- c++ - Qt 中的自定义小部件,如 QFrame
- sonos - 不从文件开头开始的有声读物章节
- javascript - 如何将html表格单元格值传递给javascript变量?
- ios - 获取键盘框架时的问题
- java - 资源类型的 JNDI 查找失败:javax.sql.DataSource
- c++ - lower_bound 函数不能正常工作?
- swift - 如何使用递归在 Swift Playground 中打印斐波那契数列