因此,我有一些<select>
元素,並且它的每個可能選項都有一個跨度內的價格。我循環遍歷所有選擇標記,我需要爲每個選項獲取所選選項的跨度值。所以這是我的代碼JavaScript/jQuery - 獲取當前元素的跨度值
<select class="form-control config-option" id="s{{ $availableCategory->index}}">
<option>{{ $availableCategory->default_option }}</option>
@foreach($availableOptions as $availableOption)
@if($availableOption->category->name == $availableCategory->name)
<option>({{ $availableOption->code }}) {{ $availableOption->name }} <span class="option-price">({{ $currency == 'EUR' ? 'EUR' : 'USD'}} {{ $availableOption->price }})</span></option>
@endif
@endforeach
</select>
我試圖通過jQuery的find()
功能按類別和標籤名稱來訪問它,但我不這樣做。
$('.config-option').each(function() {
var currentElement = $(this);
var optionPrice = currentElement.find('.option-price');
console.log(optionPrice);
});
如果我控制檯日誌optionPrice.val()
我得到未定義。那麼我怎樣才能訪問這個跨度?有沒有更好的方法來做到這一點?使用普通JavaScript的解決方案也可以。
[獲取跨度文本值]的可能的複製(HTTP:/ /stackoverflow.com/questions/10343838/get-value-of-span-text) – thatOneGuy