A
A
Alexey2019-11-05 16:00:44
WordPress
Alexey, 2019-11-05 16:00:44

How to implement a checkbox that hides the fields on the checkout page?

Task:
1. Show either field1 or field2.
2. When submitting, do not send a hidden field.

If checked: 
Поле1 
else: 
Поле2

______
Partially implemented, but when sending, if there is data in the hidden field, they are still sent.
How to implement so that only the visible field is sent?
add_action( 'woocommerce_after_checkout_form', 'custom_hide_show_field', 6);
function custom_hide_show_field 
{
?>
<script type="text/javascript">
$('input[type=checkbox]').change(function(){
    if(this.checked) {
        $('.field1').hide();
        $('.field2').show();
    } else {
        $('.field1').show();
        $('.field2').hide();        
    }
});
</script>
<?php
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Druzhaev, 2019-11-05
@alexmirg

What if ? well, or the value is first into a variable - and then cleared. And when castling - we change places? ps I remember, if the field is disabled - then they are not transferred to the post request - as option 2 $('.field1').empty().hide();

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question