Php 如何在select2中添加占位符
下面是动态生成下拉列表的php jquery代码 它工作得很好。但我必须在这个下拉列表中添加占位符 所以我添加了下面的代码,但它不起作用 我可以知道我应该在哪里添加此代码或我的代码有问题吗Php 如何在select2中添加占位符,php,jquery,jquery-select2,Php,Jquery,Jquery Select2,下面是动态生成下拉列表的php jquery代码 它工作得很好。但我必须在这个下拉列表中添加占位符 所以我添加了下面的代码,但它不起作用 我可以知道我应该在哪里添加此代码或我的代码有问题吗 $('#calc_shipping_state').one('select2:open', function(e) { $('input.select2-search__field').prop('placeholder', 'enter username or
$('#calc_shipping_state').one('select2:open', function(e) {
$('input.select2-search__field').prop('placeholder', 'enter username or city');
});
问候,,
亚历克斯
jQuery(函数($){
var国家=“”;
$(document).ready(函数(){new_state_visibility()});
函数新状态可见性(){
如果($(“#计算装运国家/地区选项:选中”).val()==国家/地区(“#计算装运国家/地区”).val()==国家/地区{
$('calc#u shipping_new_state_field')。show();
}否则{
$('calc#u shipping_new_state_field')。hide();
}
};
$('calc#u shipping_state')。打开('select2:open',函数(){
$('.select2-search__字段').attr('占位符','您的占位符');
});
如果($(“#计算装运国家/地区选项:选中”).val()==国家/地区(“#计算装运国家/地区”).val()==国家/地区{
$('calc#u shipping_state')。数据('select2')。下拉列表。$search.val($('calc#u shipping_new_state option:selected')。text()+'-');
$('calc#u shipping_state')。数据('select2')。下拉菜单。$search.trigger('input');
$('calc#u shipping_state')。触发器('query',{term});
}
} );
$('calc#u shipping_state')。打开('select2:select',函数(){
$('#calc_shipping_new_state option').filter(函数(){return$(this).text()==$('#calc_shipping_state option:selected').text().split('-')[0];).prop('selected',true);
} );
$('calc#u shipping_state')。一个('select2:open',函数(e){
$('input.select2-search__field').prop('placeholder','enterusername或city');
});
} );
我不太确定,但我认为它是带有数据占位符
属性的,所以类似这样
数据占位符=“占位符”
您可以阅读有关占位符的文档,答案如下
woocommerce_form_field( 'calc_shipping_new_state', [
'type' => 'select',
'options' => [ '' => '' ] + apply_filters( 'csz_states', $country_states ),
'class' => [ 'form-row-wide', 'address-field' ]
'placeholder' => 'State / County',
] );
您可以很容易地添加第三方“select2”脚本来完成此操作。该脚本没有帮助?@Tuhin为什么第三方代码比select2提供的占位符选项更好?此外,如果没有任何链接或指南,“您可以轻松添加”之类的评论也很少有用。
woocommerce_form_field( 'calc_shipping_new_state', [
'type' => 'select',
'options' => [ '' => '' ] + apply_filters( 'csz_states', $country_states ),
'class' => [ 'form-row-wide', 'address-field' ]
'placeholder' => 'State / County',
] );