這是爲html表單添加交互性。爲什麼我的元素只在選擇信用卡時才顯示/隱藏?
我試圖在選擇相應選項時顯示付款選擇信息。然後隱藏其他選項。所以如果選擇信用卡,信用卡顯示的信息,那麼比特幣和貝寶的信息將被隱藏。反之亦然。
,當我選擇信用卡某種原因,一切正常,
然而,當我切換到無論是PayPal或比特幣,沒有顯示在所有。我怎樣才能解決這個問題?我寧願不使用jQuery。
下面是各個JS:
// Payment Info section of the form. Display payment sections based on chosen payment option
document.getElementById("payment options").addEventListener("change", function(){
var paymentOption = document.getElementById('payment');
var paymentSelection = paymentOption.value;
var container = document.getElementById('payment-container');
var creditCard = document.getElementById('credit-card');
var bitcoin = document.getElementById('bitcoin');
var paypal = document.getElementById('paypal');
// "Credit Card" payment option isselected by default so display of the #credit-card div...
// hide the "Paypal" and "Bitcoin information.
if(paymentSelection === "credit card") {
bitcoin.style.visibility = 'hidden';
paypal.style.visibility = 'hidden';
creditCard.style.visibility = 'visible';
}if(paymentSelection === "paypal") {
console.log('paypal');
// If user selects the "PayPal" payment option, display the Paypal information, and hide the credit card + Bitcoin
bitcoin.style.visibility = 'hidden';
creditCard.style.visibility = 'hidden';
paypal.style.visibility = 'visible';
} if(paymentSelection === "bitcoin") {
console.log('bitcoin');
/// If user selects the "Bitcoin" payment option, display the Bitcoin information, and hide the credit card + paypal.
paypal.style.visibility = 'hidden';
creditCard.style.visibility = 'hidden';
bitcoin.style.visibility = 'visible';
}
});
這裏是HTML:
<fieldset id="payment options">
<legend>Payment Info</legend>
<label for="payment">I'm going to pay with:</label>
<select id="payment" name="user_payment">
<option value="credit card">Credit Card</option>
<option value="paypal">PayPal</option>
<option value="bitcoin">Bitcoin</option>
</select>
<div id="payment-container">
<div id="credit-card" class="credit-card">
<div class="col-6 col">
<label id="cc-numLbl" for="cc-num">Card Number:</label>
<input id="cc-num" name="user_cc-num" type="text">
</div>
<div class="col-3 col">
<label for="zip" id="zipLbl">Zip Code:</label>
<input id="zip" name="user_zip" type="text">
</div>
<div class="col-3 col">
<label id="cvvLbl" for="cvv">CVV:</label>
<input id="cvv" name="user_cvv" type="text">
</div>
<label>Expiration Date:</label>
<select id="exp-month" name="user_exp-month">
<option value="1">1 - January</option>
<option value="2">2 - February</option>
<option value="3">3 - March</option>
<option value="4">4 - April</option>
<option value="5">5 - May</option>
<option value="6">6 - June</option>
<option value="7">7 - July</option>
<option value="8">8 - August</option>
<option value="9">9 - September</option>
<option value="10">10 - October</option>
<option value="11">11 - November</option>
<option value="12">12 - December</option>
</select>
<select id="exp-year" name="user_exp-year">
<option value="2016">2016</option>
<option value="2017">2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
</div>
<div id="paypal">
<p>If you selected the PayPal option we'll take you to Paypal's site to set up your billing information, when you click 'Register' below.</p>
</div>
<div id="bitcoin">
<p>If you selected the Bitcoin option we'll take you to the Coinbase site to set up your billing information. Due to the nature of exchanging Bitcoin, all Bitcoin transactions will be final.</p>
</div>
</fieldset>
你在現場使用2個ID,這是壞的,你應該改變它只有一個ID –