WooCommerce Select2通过字符串开头匹配美国/加拿大等州,而不仅仅是任何匹配
WooCommerce在州和国家/地区的发货和账单地址字段中使用select2。我想这样做,当您搜索WooCommerce Select2通过字符串开头匹配美国/加拿大等州,而不仅仅是任何匹配,woocommerce,jquery-select2,dropdown,matcher,Woocommerce,Jquery Select2,Dropdown,Matcher,WooCommerce在州和国家/地区的发货和账单地址字段中使用select2。我想这样做,当您搜索ka时,最重要的结果是Kansas而不是Alaska 我查看了select2的文档,发现: 但这个解决方案适用于有孩子的情况。我不知道如何编辑文档化的示例,使其与WooCommerce一起工作。另一篇SO文章建议: function matchCustom(term, text) { console.log('matcher is running'); if (text.toUpperC
ka
时,最重要的结果是Kansas
而不是Alaska
我查看了select2的文档,发现:
但这个解决方案适用于有孩子的情况。我不知道如何编辑文档化的示例,使其与WooCommerce一起工作。另一篇SO文章建议:
function matchCustom(term, text) {
console.log('matcher is running');
if (text.toUpperCase().indexOf(term.toUpperCase()) == 0) {
return true;
}
}
jQuery( document ).ready(function() {
$(".state_select").select2({
matcher: matchCustom
});
});
使用上面的代码,当我搜索ka
时,最上面的结果是Alaska
而不是Kansas
我研究了以下问题:
您可以这样做:
my wc country select.js
):
确保将wc country select
添加为依赖项它工作正常,谢谢你,很抱歉延迟回复。这对我来说是一个积压项目。
jQuery( function( $ ){
if ( ! $().selectWoo ) {
return;
}
// Based on <https://select2.org/searching#customizing-how-results-are-matched>
function matchCustom(params, data) {
// If there are no search terms, return all of the data
if ($.trim(params.term) === '') {
return data;
}
// Do not display the item if there is no 'text' property
if (typeof data.text === 'undefined') {
return null;
}
// `params.term` should be the term that is used for searching
// `data.text` is the text that is displayed for the data object
var s = $.trim( params.term ).toLowerCase();
var s2 = $.trim( data.text ).toLowerCase();
if ( s === s2.substr( 0, s.length ) ) {
return data;
}
// Return `null` if the term should not be displayed
return null;
}
function country_select_select2() {
// Apply the custom "matcher" to the country and state drop-downs.
$( 'select.country_select:visible, select.state_select:visible' ).selectWoo( {
// We're just changing this option.
matcher: matchCustom
} );
}
country_select_select2();
$( document.body ).bind( 'country_to_state_changed', function() {
country_select_select2();
});
} );