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 關注
- 123 瀏覽
添加回答
舉報