首页 > 解决方案 > 如果在 WooCommerce 结帐页面上选中复选框,则更改送货城市字段的值

问题描述

如果选中复选框,我正在尝试为运输城市设置一个值“local-pickup”,同时在设置复选框时隐藏该字段。

当复选框被选中时,我能够隐藏该字段,但无法将字段的值更改为“local-pickup”。字段运输城市是一个选择字段。

这是我的代码:

add_action('woocommerce_after_checkout_form', 'bbloomer_conditionally_hide_show_checkout_field', 9999);

function bbloomer_conditionally_hide_show_checkout_field() {
  wc_enqueue_js("
    jQuery('#shipping_checkbox').change(function() {
      if (this.checked) {
        jQuery('#shipping_address_1_field').hide();
        jQuery('#shipping_address_2_field').hide();
        jQuery('#shipping_city_field').hide();
      } else {
        jQuery('#shipping_address_1_field').show();
        jQuery('#shipping_address_2_field').show();
        jQuery('#shipping_city_field').show();
      }
    }).keyup();
    ");
  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<label class="checkbox" data-required="0">
                        <input type="checkbox" class="input-checkbox " name="shipping_checkbox" id="shipping_checkbox" value="1">local-pickup?</label>
            
<select name="shipping_city" id="shipping_city" class="select wc-enhanced-select" data-required="1" autocomplete="address-level2"><option value="" disabled="disabled" selected="selected">select city</option><option value=""></option><option value="city1">city1</option><option value="city2">city2</option><option </select>

谢谢

标签: jquerycheckboxwoocommercecheckoutshipping

解决方案


推荐阅读