1 回答

TA贡献1848条经验 获得超10个赞
您获得最后一个选项的原因是因为您在选择选项时没有更新它。
首先,我们需要data在选项中添加属性。
<select class="custom-select" id="product-price-{{$i}}-select" name="LeaveType">
<option selected>Choose a frame...</option>
@foreach($attributes->frame as $attribute)
<option
value="{{$attribute->price}}"
data-template-id="{{$attribute->template_id}}"
data-name="{{$attribute->name}}"
data-id="{{$attribute->id}}">{{$attribute->name}}</option>
@endforeach
</select>
现在在您的.custom-select更改事件中。
<script>
$(document).ready(function () {
$('.custom-select').on('change', function () {
$('.custom-select').not(this).val('Choose a frame...');
var current = $(this).attr('id');
var res = current.split("-select");
$('.' + res[0]).html($(this).val());
// Get the data from selected option
var selected_option = $(this).find('option:selected');
var template_id = selected_option.data('template-id');
var product_name = selected_option.data('name');
// Now assign it to hidden input field
$('input[name="product_name"]').val(product_name);
$('input[name="sku"]').val(template_id);
$('input[name="price"]').val($(this).val());
});
});
</script>
而已。这应该可以解决问题。我没有测试它。如果有问题让我知道。
还有一件事。您可以从此表单中删除值,因为它会在您选择一个选项时被填充。
- 1 回答
- 0 关注
- 107 浏览
添加回答
举报