简体   繁体   English

使用 jquery 无法在输入字段内获取 Woocommerce 计费电话

[英]can't get Woocommerce billing phone inside input field using jquery

I have a CF7 form and need to get Woocommerce billing phone number and display it inside phone field on page load using jQuery function.我有一个 CF7 表格,需要获取Woocommerce 计费电话号码,并使用 jQuery function 在页面加载时的电话字段中显示它。 I used this code in header area:我在 header 区域使用了这个代码:

// Get billing_phone_number On Quote Form

jQuery(document).ready(function(){
        jQuery('#mk-order-tel').val("<?php echo get_user_meta( get_current_user_id(), 'billing_phone', true ) ?>");
    });

But it returns raw php code ( <?php echo get_user_meta( get_current_user_id(), 'billing_phone', true )?> ) instead of phone number;但它返回原始 php 代码( <?php echo get_user_meta( get_current_user_id(), 'billing_phone', true )?> )而不是电话号码; when I use same php code directly in php files of Woocommerce it works correctly.当我直接在 Woocommerce 的 php 文件中使用相同的 php 代码时,它可以正常工作。 What should I do to run a php syntax through the jQuery?我应该怎么做才能通过 jQuery 运行 php 语法?

EDIT编辑


To escape of above issue, I tried to get data by using ajax but this method does not work too:为了避免上述问题,我尝试使用 ajax 获取数据,但这种方法也不起作用:

in functions.php:在函数中。php:

/* Get Customer Phone Number */

function my_ajax_handler(){
    global $woocommerce;
    $phone = get_user_meta( get_current_user_id(), 'billing_phone', true );
     ?>
    <script type="text/javascript">
    jQuery( function($){
    jQuery('#mk-order-tel').val("<?php echo json_encode($phone); ?>");
    });
    </script>
    <?php
}

add_action( 'wp_ajax_call_my_ajax_handler', 'my_ajax_handler' );
add_action( 'wp_ajax_nopriv_call_my_ajax_handler', 'my_ajax_handler' );


function my_quote_scripts() {
  wp_enqueue_script( 'my-ajax-script', get_stylesheet_directory_uri() . '/js/quote.js', array('jquery') );
  wp_localize_script( 'my-ajax-script', 'my_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
}
add_action( 'wp_enqueue_scripts', 'my_quote_scripts' );

in quote.js:在quote.js:

jQuery.ajax({
    
    type:"POST",
    url: my_ajax_object.ajax_url,
    data: { 'action': 'call_my_ajax_handler' }
    
})

What else can I do?我还可以做些什么?

This should work, you're getting an exception because of quotes这应该可行,由于引号,您会遇到异常

const tel = <?php echo get_user_meta( get_current_user_id(), 'billing_phone', true ) ?>;
jQuery(document).ready(function(){
   jQuery('#tel').val(tel);
});

Or you can just replace single quotes to double或者您可以将单引号替换为双引号

jQuery(document).ready(function(){
        jQuery('#tel').val("<?php echo get_user_meta( get_current_user_id(), 'billing_phone', true ) ?>");
    });

Simply using this code in functions.php of child theme can fetch customer phone and display inside input text field:只需在函数中使用此代码。子主题的 php 可以获取客户电话并在输入文本字段中显示:

/* Get Customer Phone Number On Quote Form */

function my_jquery_var() {
    if ( $yourtel = get_user_meta( get_current_user_id(), 'billing_phone', true ) ) { 
        
        echo '<script type="text/javascript">
        var yourTel = "' . $yourtel . '";
        document.getElementById("mk-order-tel").value = yourTel;
        </script>' . "\n";
        
    }
}
add_action( 'wp_footer', 'my_jquery_var' );

Note : JavaScript should be in footer area.注意:JavaScript 应该在页脚区域。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用JQuery获取输入字段的值 - Can't get value of input field using JQuery 通过jQuery获取Magento中计费街输入字段的值 - Get the value of the billing street input field in Magento via jQuery jQuery-在输入字段上使用它-无法使变量超出功能范围,因此我可以使用它 - jquery - using it on an input field - can't get variable out of function so I can use it jQuery UI自动完成-无法获取在输入字段中键入的数据 - JQuery UI AutoComplete - can't get data typed in the input field jQuery无法从动态创建的字段获取输入值 - JQuery can't get input value from dynamically created field WooCommerce:在结账时将账单地址字段的输入大写 + 更新订单元数据? - WooCommerce: Capitalize input to the billing address field at checkout + update the order meta? jQuery屏蔽输入| 正确输入电话号码字段后无法删除蒙版 - jQuery Masked Input | Can't remove mask after correct entry to the phone number field 如果使用虚拟键盘,Jquery如何在输入字段中获取值 - How Jquery can get value in input field if using virtual keyboard 使用javascript无法获取输入字段值? - Can't get input field value using javascript? jQuery无法访问表中的输入字段 - jQuery can't access input field in table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM