如何通过select2设置值重置时的占位符。在我的示例中,如果单击了位置或坡度选择框,并且我的select2的值大于select2的值,则应重置并显示默认占位符。此脚本正在重置值,但不会显示占位符
$("#locations, #grade ").change(function() {
$('#e6').select2('data', {
placeholder: "Studiengang wählen",
id: null,
text: ''
});
});
$("#e6").select2({
placeholder: "Studiengang wählen",
width: 'resolve',
id: function(e) {
return e.subject;
},
minimumInputLength: 2,
ajax: {
url: "index.php?option=com_unis&task=search.locator&tmpl=component&<?php echo JSession::getFormToken() ?>=1",
dataType: 'json',
data: function(term, page) {
return {
q: term, // search term
g: $('#grade option:selected').val(),
o: $('#locations option:selected').val()
};
},
results: function(data, page) {
return {
results: data
};
}
},
formatResult: subjectFormatResult,
formatSelection: subjectFormatSelection,
dropdownCssClass: "bigdrop",
escapeMarkup: function(m) {
return m;
}
});
发布于 2013-10-22 22:11:23
您必须将select2定义为
$("#customers_select").select2({
placeholder: "Select a customer",
initSelection: function(element, callback) {
}
});
要重置select2,请执行以下操作
$("#customers_select").select2("val", "");
发布于 2016-03-03 21:33:35
被接受的答案在我的情况下不起作用。我试着这样做,它起作用了:
定义select2:
$("#customers_select").select2({
placeholder: "Select a State",
allowClear: true
});
或
$("#customers_select").select2({
placeholder: "Select a State"
});
要重置,请执行以下操作:
$("#customers_select").val('').trigger('change')
或
$("#customers_select").empty().trigger('change')
发布于 2019-08-27 09:54:12
对于Select2版本4.0.9,这对我来说是有效的:
$( "#myselect2" ).val('').trigger('change');
https://stackoverflow.com/questions/17957040
复制相似问题