基于WP dev doc: including css javascript已经在默认wordpress脚本中注册的Jquery Autocomplete UI,我只需要调用它:
此代码转到主题文件夹上的 function.php:
// Load ui.autocomplete javascript and style
function add_theme_scripts() {
if ( is_checkout() ) {
wp_enqueue_style( 'jquery-ui-base', 'https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/base/jquery-ui.css', false,'1.0','all');
wp_enqueue_script( 'jquery-ui-autocomplete' );
wp_enqueue_script( 'resource.zip', get_theme_file_uri() . '/js/resource.zip.js', array( 'jquery', 'jquery-ui-autocomplete' ),'1.1',true);
}
}
add_action( 'wp_enqueue_scripts', 'add_theme_scripts' );
这是resource.zip.js:
( function( $ ) {
var availableZip = [
'ZIP01',
'ZIP02',
'ZIP03'
];
$( "#billing_postcode" ).autocomplete({
source: availableZip,
select: function (event, ui) {
$( "#billing_postcode" ).val( ui.item.value );
jQuery('body').trigger('update_checkout');
return false;
}
});
$( "#billing_postcode" ).on( "autocompleteselect", function( event, ui ) {
jQuery('body').trigger('update_checkout');
return false;
} );
} )( jQuery );
发现运费更新的js目标函数触发器是'update_checkout'。这段代码对我有用,它在桌面上用鼠标、键盘和带触摸屏的设备进行了测试。