我有一段html允许用户在下拉选项中选择结算字词。我需要编写javascript来获取所选选项的值,并将其用于不同的部分。
我的HTML:
<div class="payment-term">
<label>Billing period:</label>
<select id="billing-price" name="billing-term" class="selectpicker">
<option value="200">12 Months: $200/mo. (Save 20%)</option>
<option value="250">1 Month: $250/mo.</option>
</select>
</div>
<div class="card-charge-info">
Your card will be charged $<span id="payment-total"></span> now, and your subscription will bill $<span id="payment-total"></span> every month thereafter. You can cancel or change plans anytime.
</div>
我已启动的Javascript:
var e = document.getElementById("#payment-term");
var stringPmt = e.options[e.selectedIndex].value;
我开始的我的JSFIDDLE:http://jsfiddle.net/rynslmns/DcJ4f/1/
答案 0 :(得分:1)
您需要在选择框中附加一个事件,并分别更新两个价格(使用自己的ID)。
var select = document.getElementById('billing-price');
select.addEventListener('change', updatePrice, false);
function updatePrice(e) {
var price = select.value;
document.getElementById('payment-total').innerText = price;
document.getElementById('payment-rebill').innerText = price;
}
这是你的小提琴:http://jsfiddle.net/DcJ4f/2/
答案 1 :(得分:0)
这将为您提供所选的值
var e = document.getElementById("billing-price").value;