要檢查值在if語句,你必須把它
$(document).on("click", "input[name=newBillingAddress]:radio", function(){
$("input:radio[name=newBillingAddress]:checked").val('checked');
$("input:radio[name=newBillingAddress]:not(:checked)").val('unchecked');
// removed extra }
if($('#existingBillingRadio').val() === 'checked'){
$(this).parent().find('.toggleBlock').first().slideDown();
}
});
它並不真正清楚你真正想要的,但單選按鈕有一個檢查屬性,而不是一個值比較。所以你應該檢查的是
$(document).on("click", "input[name=newBillingAddress]:radio", function(){
// $("input:radio[name=newBillingAddress]:checked").prop('checked', true);
// $("input:radio[name=newBillingAddress]:not(:checked)").prop('checked', false);
// the above lines don't make sense in this case anymore.
// removed extra }
if($('#existingBillingRadio').is(':checked')){
$(this).parent().find('.toggleBlock').first().slideDown();
}
});
'$(this)'是什麼? –
您應該使用'prop'方法而不是'val'來檢查複選框。 http://api.jquery.com/prop/ – MasNotsram
@DhavalMarthak此代碼中的所有地方,並非在所有代碼中都處處可見。就在處理複選框和收音機時。 – MasNotsram