我使用以下代码隐藏所选付款方式的信用卡号字段。它完美地隐藏了该字段,但是当该字段被隐藏并且我单击“下订单”按钮时,它仍然显示“需要信用卡号”。
// show hide credit card fields start
add_action( 'wp_footer', 'conditionally_show_hide_card_number_field' );
function conditionally_show_hide_card_number_field(){
// Only on checkout page
if ( is_checkout() && ! is_wc_endpoint_url() ) :
?>
<script>
jQuery(function($){
var a = 'input[name="payment_method"]',
b = a + ':checked',
c = '#credit_card_number_field'; // The checkout field <p> container selector
// Function that shows or hide checkout fields
function showHide( selector = '', action = 'show' )
{
if( action == 'show' )
$(selector).show( 200, function(){
$(this).addClass("validate-required");
});
else
$(selector).hide( 200, function(){
$(this).removeClass("validate-required");
});
$(selector).removeClass("woocommerce-validated");
$(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
}
// Initialising: Hide if choosen payment method is "credit_card"
if( $(b).val() == 'elavon_converge_credit_card' )
showHide( c, 'hide' );
else
showHide( c );
// Live event (When payment method is changed): Show or Hide based on "credit_card"
$( 'form.checkout' ).on( 'change', a, function() {
if( $(b).val() == 'elavon_converge_credit_card' )
showHide( c, 'hide' );
else
showHide( c );
});
});
</script>
<?php
endif;
}
// show hide credit card fields ends
请建议更改代码以解决此问题。