Woocomerce 自定义字段显示基于单选按钮选择

问题描述

我有 3 个单选按钮和 6 个字段,需要根据单选按钮的选择来显示。有人可以帮我吗?

 if( !function_exists( 'custom_checkout_question_field' ) ) {
    
      function custom_checkout_question_field( $checkout ) {
    
        echo "<div class='custom-question-field-wrapper custom-question-1'>";
       woocommerce_form_field( 'custom_question_field',array(
          'type'            => 'radio','required'        => true,'class'           => array('custom-question-field','form-row-wide'),'options'         => array(
            'privat'         => 'Privatpersona','juri'    => 'Juridiska','comp'    => 'Uznemums',),$checkout->get_value( 'custom_question_field' ) );
    
        woocommerce_form_field( 'custom_Name',array(
          'type'            => 'text','label'           => 'Vārds','class'           => array('custom-Name-field',$checkout->get_value( 'custom_Name' ) );
        
        woocommerce_form_field( 'custom_Surname','label'           => 'Uzvārds','class'           => array('custom-Surname-field',$checkout->get_value( 'custom_Surname' ) );
    
        woocommerce_form_field( 'custom_PK','label'           => 'Personas Kods','class'           => array('custom-PK-field',$checkout->get_value( 'custom_PK' ) );
    
        woocommerce_form_field( 'custom_registrationnumber','label'           => 'Reģistrācijas numurs','class'           => array('custom-registrationnumber-field',$checkout->get_value( 'custom_registrationnumber' ) );
    
        woocommerce_form_field( 'custom_PVNnumber','label'           => 'PVN maksātāja numurs','class'           => array('custom-PVNnumber-field',$checkout->get_value( 'custom_PVNnumber' ) );
    
        woocommerce_form_field( 'custom_companyname','label'           => 'Uzņēmuma nosaukums','class'           => array('custom-companyname-field',$checkout->get_value( 'custom_companyname' ) );
    
    
        echo "</div>";
    
      }
    
      add_action( 'woocommerce_after_checkout_billing_form','custom_checkout_question_field' );
    }
    
    if( !function_exists( 'custom_question_conditional_javascript' ) ) {
      function custom_question_conditional_javascript() {
        ?>
        <script type="text/javascript">
        (function() {
    
          // Check if jquery exists
          if(!window.jQuery) {
            return;
          };
    
          var $ = window.jQuery;
    
          $(document).ready(function() {
    
            var
                questionField       = $('custom-question-field'),NameField                = $('custom-Name-field'),SurnameField             = $('custom-Surname-field'),PKField                  = $('custom-PK-field'),registrationnumberField  = $('custom-registrationnumber-field'),PVNnumberField           = $('custom-PVNnumber-field'),companynameField         = $('custom-companyname-field');
    
            }
    
            function toggleVisibleFields() {
                var selectedAnswer = questionField.find('input:checked').val();
                if(selectedAnswer === 'privat') {
                NameField.show();
                SurnameField.show();
                PKField.hide();
                registrationnumberField.hide();
                PVNnumberField.hide();
                companynameField.hide();
              } else if (selectedAnswer === 'juri') {
                NameField.hide();
                SurnameField.hide();
                PKField.hide();
                registrationnumberField.show();
                PVNnumberField.show();
                companynameField.show();
              } else if(selectedAnswer === 'comp') {
                NameField.hide();
                SurnameField.hide();
                PKField.hide();
                registrationnumberField.show();
                PVNnumberField.hide();
                companynameField.show();
              } else {
                NameField.hide();
                SurnameField.hide();
                PKField.hide();
                registrationnumberField.hide();
                PVNnumberField.hide();
                companynameField.hide();
              }
        }
              
    
            $(document).on('change','input[name=custom_question_field]',toggleVisibleFields;
            $(document).on('updated_checkout',toggleVisibleFields;
            toggleVisibleFields();
    
          });
        })();
        </script>
        <?PHP
      }
    
      add_action( 'wp_footer','custom_question_conditional_javascript',1000 );

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)