问题描述
您好,我在 WordPress 上制作网站,我需要设置手机掩码 on checkout page. 我从 GitHub 获得文件 jquery.maskedinput.min.js 并在途中将其上传到托管:
wp-content/themes/woodmart-child/js/
然后我在子主题的function.php中添加了脚本:
add_action('wp_enqueue_scripts','my_maskedinput',10);
function my_maskedinput() {
if (is_checkout()) {
wp_enqueue_script('maskedinput','/wp-content/themes/woodmart-child/js/jquery.maskedinput.min.js',array('jquery'));
add_action( 'wp_footer','masked_script',999);
}
}
function masked_script() {
if ( wp_script_is( 'jquery','done' ) ) {
?>
<script type="text/javascript">
jQuery( function( $ ) {
$("#billing_phone").mask("+7(999)999-99-99");
});
</script>
<?php
}
}
但是电话掩码没有显示在结帐页面上。我检查了 DevTools,代码
<script type="text/javascript">
jQuery( function( $ ) {
$("#billing_phone").mask("+7(999)999-99-99");
});
</script>
被添加到 html 文件中。但错误在哪里?我做错了什么?
upd:我尝试通过 str_replace 实现它(但它也不起作用):
add_filter( 'woocommerce_form_field',20,4 );
function my_maskedinput( $field,$key,$args,$value ) {
// Only on checkout page
if( is_checkout() && ! is_wc_endpoint_url() ) {
$phone = ' <input type="tel" class="input-text " name="billing_phone" id="billing_phone" placeholder="Номер телефона" value autocomplete="tel">';
$field = str_replace( $phone,' <input type="tel" class="input-text" name="billing_phone" id="billing_phone" placeholder="Номер телефона" value autocomplete="tel" mask="+7 (999) 999-99-99">',$field );
}
return $field;
}
解决方法
我检查文件 jquery.maskedinput.min.js 并且它不包含正确的脚本。 我从 here 获得文件并将其上传到主机上。然后它工作!