Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/256.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将postalcode更改为下拉菜单_Javascript_Php_Woocommerce - Fatal编程技术网

Javascript 将postalcode更改为下拉菜单

Javascript 将postalcode更改为下拉菜单,javascript,php,woocommerce,Javascript,Php,Woocommerce,我试图更改签出页面中的postalcode,并将其限制为特定选项,所以我使用了此代码 $address_字段['postcode']['default']=12652 返回$address\u字段; 实际上一切都很好,但主要问题是选项的值我用它来计算运费,但没有更新。。我正在寻找js的东西,以获得postalcode的选择器,但我无法找到或解决此问题。。有什么帮助吗?请阅读并尝试改进您的。另外,尝试改进代码的格式。您还可以在更改事件之外将下拉值设置为帐单字段值,以防用户重新加载浏览器 //

我试图更改签出页面中的postalcode,并将其限制为特定选项,所以我使用了此代码

$address_字段['postcode']['default']=12652

返回$address\u字段;


实际上一切都很好,但主要问题是选项的值我用它来计算运费,但没有更新。。我正在寻找js的东西,以获得postalcode的选择器,但我无法找到或解决此问题。。有什么帮助吗?

请阅读并尝试改进您的。另外,尝试改进代码的格式。您还可以在更改事件之外将下拉值设置为帐单字段值,以防用户重新加载浏览器 // Hook in add_filter('woocommerce_default_address_fields','custom_override_default_address_fields');

// Our hooked in function - $fields is passed via the filter! function custom_override_default_address_fields($address_fields) {

// Just changing what is needed to turn it into a select.
// Modify other options if you need.
$address_fields['postcode']['type'] = 'select';
$address_fields['postcode']['options'] = array('12652' => "Area 1",
                                               '21511' => "Area 2",
                                               '42511' => "Area 3");
#[ FIXED ]
Here is what i did and it worked fine with me..


Step 1 => add this filter in order to add new field in checkout form.

    add_filter( 'woocommerce_checkout_fields' , 'add_address_type_field' );
    function add_address_type_field( $fields ) {

     $address_type_field = array(
        'type'      => 'select',
        'required'  => false,
        'class'     => array('address-field', 'form-row-wide', 'validate-addresstype'),
        'clear'     => true,
        'options'   => array(
                        'postCode' => 'Area 1',
                        'postCode' => 'Area 2',
                    ),
     );

     $fields['billing']['billing_address_type']   = $address_type_field;

     return $fields;
    }



Step 2 => using css to hide default postal code input 



#billing_postcode{
   display:none;    
}


Step 3 => using JavaScript Events to make eventlistener 'Change' .. once the select value change event assign this value to default post code input by adding this code in form-billing.php


document.querySelector('#billing_address_type').addEventListener("change", function(){
        document.querySelector('input#billing_postcode').value = 
        document.querySelector('#billing_address_type').value;
        jQuery('body').trigger('update_checkout');
    });