gpt4 book ai didi

javascript - 如果下拉菜单改变,金额也会改变 PHP+JS

转载 作者:行者123 更新时间:2023-12-03 02:44:38 25 4
gpt4 key购买 nike

在下表中,我有 2 个项目,名为“夹克”和“制服”。每个项目都有一个下拉菜单,用于选择所需的数量。我想要发生的是当数量改变时金额自动改变。例如,一套制服的单价是1,400比索。如果用户想购买 2 件制服,金额将自动从 1,400 比索更改为 2,800 比索。对于“夹克”,数量变化时金额会自动变化。一件夹克的单价是700比索。因此,当数量从 1 更改为 2 时,金额从 700 比索更改为 1,400 比索。我不明白为什么“统一”没有发生同样的情况,也就是说,当数量改变时,金额不会改变。任何帮助将不胜感激。请参阅下面我的代码。非常感谢。

这是我的代码:
PHP

 @foreach($data['tuition_other'] as $other)
<tr>
<td>{{Hashids::encode($other->id)}}</td>
<td>{{$other->item_name}}</td>
<td id="item_amt">{{number_format($other->item_amount,2,'.',',')}}</td>
<td>
<select name="item_quantity" class="iqty_class" id="iqty_id">
@for ($i=1; $i<=$other->max_quantity; $i++)
<option value="{{ $i }}">{{ $i }}</option>
@endfor
</select>
<input type="hidden" id="item_val" value="{{ $other->item_amount }}">
</td>
<? $total_other += $other->item_amount; ?>
</tr>
@endforeach

JS

$('body').on('change','.iqty_class', function(){
var item_amt = $('#item_val').val();
var item_qty = $('.iqty_class').val();
var total_amt = item_amt * item_qty;
var c = total_amt.toLocaleString('en-US', {minimumFractionDigits: 2});
$("#item_amt").html(c);
});

最佳答案

尝试这样做:

Blade

@foreach($data['tuition_other'] as $other)
<tr>
<td>{{Hashids::encode($other->id)}}</td>
<td>{{$other->item_name}}</td>
<td class="item_amt">{{number_format($other->item_amount,2,'.',',')}}</td>
<td>
<select name="item_quantity" class="iqty_class" id="iqty_id">
@for ($i=1; $i<=$other->max_quantity; $i++)
<option value="{{ $i }}">{{ $i }}</option>
@endfor
</select>
<input type="hidden" class="item_val" value="{{ $other->item_amount }}">
</td>
<? $total_other += $other->item_amount; ?>
</tr>
@endforeach

JS:

$('body').on('change','.iqty_class', function(){
var item_amt = $(this).parent().children('.item_val').val();
var item_qty = $(this).val();
var total_amt = item_amt * item_qty;
var c = total_amt.toLocaleString('en-US', {minimumFractionDigits: 2});
$(this).parent().parent().children('.item_amt').html(c);
});

关于javascript - 如果下拉菜单改变,金额也会改变 PHP+JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48150767/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com