尝试将相同的名称属性设置为您的radiobuttons
. 另外,删除class="hidden"
给定的#card
DOMElement,因为您希望首先选择信用卡选项(查看您的 JavaScript):
HTML
<div id="paymentchoice">Creditcard
<input type="radio" name="paymentProcessor" id="creditcard" value="creditcard">Paypal</input>
<input type="radio" name="paymentProcessor" id="paypal" value="paypal" checked="checked" />
</div>
<div id="card" >
<input type="text" name="cardname">
<input type="text" name="cardnum">
<input type="text" name="cardcode">
<select name="month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
</select>
<select name="year">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="fiat">Fiat</option>
<option value="audi">Audi</option>
</select>
</div>
<input type="submit" value="Submit">
JavaScript/jQuery
// IF JAVASCRIPT ENABLED...
/*Add the change() function FIRST, since you set the creditcard as selected option just after that */
// unhide card options
$('input[name="paymentProcessor"]').change(function () {
if ($('#creditcard').is(':checked')) $('#card').removeClass('hidden');
else $('#card').addClass('hidden');
});
// select card option by default
$('#creditcard').prop("checked", true);
// unhide payment options
$('#paymentchoice').removeClass('hidden');