【问题标题】:WooCommerce Checkout - Input required if checkbox is checkedWooCommerce Checkout - 如果选中复选框,则需要输入
【发布时间】:2021-07-01 21:21:53
【问题描述】:

在我的 WooCommerce 结帐表单中,我有一个额外的复选框,在选中时会显示新的输入。 我希望在选中复选框时需要此输入。 我可以在不直接干扰表单代码的情况下使用 JavaScript 以某种方式解决它吗?

我附上表格的截图: screenshot1, screenshot2.

您可能对如何正确地做到这一点有其他想法?

<p class="form-row form-row-first validate-required woocommerce-invalid woocommerce-invalid-required-field" id="billing_first_name_field" data-priority="10">
<label for="billing_first_name" class="">First name&nbsp;<abbr class="required" title="pole wymagane">*</abbr></label>
<span class="woocommerce-input-wrapper">
<input type="text" class="input-text " name="billing_first_name" id="billing_first_name" placeholder="" value="Dominik" autocomplete="given-name">
</span>
</p>
<p class="form-row form-row-last validate-required woocommerce-invalid woocommerce-invalid-required-field" id="billing_last_name_field" data-priority="20">
<label for="billing_last_name" class="">Last name&nbsp;<abbr class="required" title="pole wymagane">*</abbr></label>
<span class="woocommerce-input-wrapper">
<input type="text" class="input-text " name="billing_last_name" id="billing_last_name" placeholder="" value="Test" autocomplete="family-name">
</span>
</p>
<p class="form-row form-row-wide" id="billing_company_field" data-priority="30">
<label for="billing_company" class="">Company name&nbsp;<span class="optional">(optional)</span></label>
<span class="woocommerce-input-wrapper">
<input type="text" class="input-text " name="billing_company" id="billing_company" placeholder="" value="" autocomplete="organization">
</span>
</p>
<p class="form-row form-row-wide woocommerce-validated" id="billing_invoice_ask_field" data-priority="30">
<span class="woocommerce-input-wrapper">
<label class="checkbox">
<input type="checkbox" class="input-checkbox " name="billing_invoice_ask" id="billing_invoice_ask" value="1"> I want to receive an invoice<span class="optional">(optional)</span>
</label>
</span>
</p>
<p class="form-row form-row-wide" id="billing_vat_number_field" data-priority="30" style="display: none;">
<label for="billing_vat_number" class="">VAT Number</label>
<span class="woocommerce-input-wrapper">
<input type="text" class="input-text " name="billing_vat_number" id="billing_vat_number" placeholder="" value="">
</span>
</p>

【问题讨论】:

  • 一般你可以在复选框中添加一个input监听事件,然后设置/取消设置所需的属性,是的。
  • 如果您提供包含所有元素的 html 表单(隐藏到)我可以为 jquery 提供帮助
  • @NikitaTSB 我在帖子中添加了表单代码。
  • @Dominik 我编辑了我的答案

标签: javascript php wordpress woocommerce


【解决方案1】:

$('.input-checkbox').change(function(){
      $('#billing_vat_number_field').toggle();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<p class="form-row form-row-first validate-required woocommerce-invalid woocommerce-invalid-required-field" id="billing_first_name_field" data-priority="10">
<label for="billing_first_name" class="">First name&nbsp;<abbr class="required" title="pole wymagane">*</abbr></label>
<span class="woocommerce-input-wrapper">
<input type="text" class="input-text " name="billing_first_name" id="billing_first_name" placeholder="" value="Dominik" autocomplete="given-name">
</span>
</p>
<p class="form-row form-row-last validate-required woocommerce-invalid woocommerce-invalid-required-field" id="billing_last_name_field" data-priority="20">
<label for="billing_last_name" class="">Last name&nbsp;<abbr class="required" title="pole wymagane">*</abbr></label>
<span class="woocommerce-input-wrapper">
<input type="text" class="input-text " name="billing_last_name" id="billing_last_name" placeholder="" value="Test" autocomplete="family-name">
</span>
</p>
<p class="form-row form-row-wide" id="billing_company_field" data-priority="30">
<label for="billing_company" class="">Company name&nbsp;<span class="optional">(optional)</span></label>
<span class="woocommerce-input-wrapper">
<input type="text" class="input-text " name="billing_company" id="billing_company" placeholder="" value="" autocomplete="organization">
</span>
</p>
<p class="form-row form-row-wide woocommerce-validated" id="billing_invoice_ask_field" data-priority="30">
<span class="woocommerce-input-wrapper">
<label class="checkbox">
<input type="checkbox" class="input-checkbox " name="billing_invoice_ask" id="billing_invoice_ask" value="1"> I want to receive an invoice<span class="optional">(optional)</span>
</label>
</span>
</p>
<p class="form-row form-row-wide" id="billing_vat_number_field" data-priority="30" style="display: none;">
<label for="billing_vat_number" class="">VAT Number</label>
<span class="woocommerce-input-wrapper">
<input type="text" class="input-text " name="billing_vat_number" id="billing_vat_number" placeholder="" value="">
</span>
</p>

玩玩:

$('.check').change(function(){
   var is_hidden = $('input[type="text"]').prop('hidden');
   var make_hidden = true;
   if (is_hidden === true){
       make_hidden = false;
   }
   $('input[type="text"]').prop('hidden', make_hidden);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="checkbox" class="check"><label>Check</label>

<input type="text" hidden>

【讨论】:

    【解决方案2】:

    我用下面的脚本解决了我的问题

    jQuery(function ($) {
                    $('#billing_invoice_ask').change(function(){
        if($(this).is(":checked")) {
            $('#billing_vat_number_field').addClass('validate-required');
        } else {
            $('#billing_vat_number_field').removeClass('validate-required');
        }
    });
    });
    

    【讨论】:

      猜你喜欢
      • 2013-12-24
      • 1970-01-01
      • 2018-01-23
      • 2018-06-17
      • 1970-01-01
      • 1970-01-01
      • 2011-09-28
      • 2017-01-08
      • 1970-01-01
      相关资源
      最近更新 更多