我正在尋找一種方法來在我的下拉列表中選擇一個選項,並通過該值獲取data-item-price
並更改下一個input.BOO_item_price
值。更改jQuery中的選擇更改後的輸入值
我有這樣的:
<table class="table table-striped table-bordered listItem">
<tbody>
<tr>
<th width="20%">Quantity</th>
<th width="50%">Item</th>
<th width="20%">Price ($ CAN)</th>
<th width="10%"></th>
</tr>
<tr>
<td>
<input type="text" class="form-control BOO_item_quantity">
</td>
<td>
<select class="form-control BOO_item_id">
<option value="">Select</option>
<option value="18" data-item-price="3">Coca</option>
<option value="20" data-item-price="2">Sprite</option>
</select>
</td>
<td>
<div class="col-sm-12">
<input type="text" class="form-control BOO_item_price">
</div>
</td>
<td>
<button type="button" class="btn btn-default removeItem"><i class="fa fa-times"></i></button>
</td>
</tr>
</tbody>
</table>
我有這個代碼的嘗試:
$('select.BOO_item_id').on('change', function() {
var price = $(this).find(':selected').data('item-price');
$(this).children('input.BOO_item_price').val(price);
});
非常感謝您的回覆和細節。它有很多幫助。您的解決方案解決了我的問題。 –