How do I set the placeholder on value reset by select2. In my example If locations or grade select boxes are clicked and my select2 has a value than the value of select2 should reset and show the default placeholder. This script is resetting the value but won't show the placeholder
如何通过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;
}
});
28 个解决方案
#1
183
You must define the select2 as
必须将select2定义为
$("#customers_select").select2({
placeholder: "Select a customer",
initSelection: function(element, callback) {
}
});
To reset the select2
重设select2
$("#customers_select").select2("val", "");
#2
92
Select2 has changed their API:
Select2改变了他们的API:
Select2: The
select2("val")
method has been deprecated and will be removed in later Select2 versions. Use $element.val() instead.Select2: Select2(“val”)方法已被废弃,将在以后的Select2版本中删除。使用$ element.val()。
The best way to do this now is:
现在最好的办法是:
$('#your_select_input').val('');
Edit: December 2016 Comments suggest that the below is the updated way to do this:
编辑:2016年12月评论建议以下是更新的方式:
$('#your_select_input').val([]);
#3
67
The accepted answer does not work in my case. I'm trying this, and it's working:
在我的情况下,公认的答案是无效的。我正在尝试这个,它正在起作用:
Define select2:
定义select2:
$("#customers_select").select2({
placeholder: "Select a State",
allowClear: true
});
or
或
$("#customers_select").select2({
placeholder: "Select a State"
});
To reset:
重置:
$("#customers_select").val('').trigger('change')
or
或
$("#customers_select").empty().trigger('change')
#4
19
The only thing can work for me is:
唯一对我有用的是:
$('select2element').val(null).trigger("change")
I'm using version 4.0.3
我用4.0.3版本
#5
14
Select2 uses a specific CSS class, so an easy way to reset it is:
Select2使用特定的CSS类,因此重置它的一种简单方法是:
$('.select2-container').select2('val', '');
And you have the advantage of if you have multiple Select2 at the same form, all them will be reseted with this single command.
如果在同一个表单中有多个Select2,那么所有的选项都将通过这个命令被重新设置。
#6
12
Use this :
用这个:
$('.select').val([]).trigger('change');
#7
7
TO REMOVE SELECTED VALUE
删除选中的值
$('#employee_id').select2('val','');
TO CLEAR OPTION VALUES
清除选项值
$('#employee_id').html('');
#8
5
I know this is kind of an old question, but this works for the select2 version 4.0
我知道这是一个老问题,但这适用于select2版本4.0。
$('#select2-element').val('').trigger('change');
or
或
$('#select2-element').val('').trigger('change.select2');
if you have change events bound to it
如果您有更改事件绑定到它
#9
5
Firstly you must define select2 like this:
首先,您必须这样定义select2:
$('#id').select2({
placeholder: "Select groups...",
allowClear: true,
width: '100%',
})
To reset select2, simply you may use the following code block:
要重置select2,只需使用以下代码块:
$("#id > option").removeAttr("selected");
$("#id").trigger("change");
#10
3
For users loading remote data, this will reset the select2 to the placeholder without firing off ajax. Works with v4.0.3:
对于加载远程数据的用户,这将在不启动ajax的情况下将select2重置为占位符。适用于v4.0.3:
$("#lstProducts").val("").trigger("change.select2");
#11
3
Use following to configure select2
使用以下命令配置select2
$('#selectelementid').select2({
placeholder: "Please select an agent",
allowClear: true // This is for clear get the clear button if wanted
});
And to clear select input programmatically
并以编程方式清除select输入。
$("#selectelementid").val("").trigger("change");
$("#selectelementid").trigger("change");
#12
2
Following the recommended way of doing it. Found in the documentation https://select2.github.io/options.html#my-first-option-is-being-displayed-instead-of-my-placeholder (this seems to be a fairly common issue):
遵循推荐的方法。在文档中发现https://select2.github.io/options。html#my-first- is being-display -instead of- of-my-placeholder(这似乎是一个相当普遍的问题):
When this happens it usually means that you do not have a blank <option></option>
as the first option in your <select>
.
当这种情况发生时,通常意味着您在
as in:
如:
<select>
<option></option>
<option value="foo">your option 1</option>
<option value="bar">your option 2</option>
<option value="etc">...</option>
</select>
#13
2
I was also having this problem and it stems from val(null).trigger("change");
throwing a No select2/compat/inputData
error before the placeholder gets reset. I solved it by catching the error and setting the placeholder directly (along with a width). Note: If you have more than one you will need to catch each one.
我也有这个问题它来自val(null)。trigger(“改变”);在占位符重置之前抛出一个No select2/compat/inputData错误。我通过捕获错误并直接设置占位符(以及宽度)来解决这个问题。注意:如果你有一个以上的,你需要抓住每一个。
try{
$("#sel_item_ID").select2().val(null).trigger("change");
}catch(err){
console.debug(err)
}
try{
$("#sel_location_ID").select2().val(null).trigger("change");
}catch(err){
console.debug(err)
}
$('.select2-search__field')[0].placeholder='All Items';
$('.select2-search__field')[1].placeholder='All Locations';
$('.select2-search__field').width(173);
I'm running Select2 4.0.3
我跑步Select2 4.0.3
#14
2
You can clear te selection by
你可以通过
$('#object').empty();
But it wont turn you back to your placeholder.
但它不会让你回到你的占位符。
So its a half solution
这是一半的解
#15
1
I have tried above solutions but none worked with version 4x.
我已经尝试过以上的解决方案,但是没有一个能与4x版本兼容。
What i want to achieve is: clear all options but don't touch the placeholder. This code works for me.
我想实现的是:清除所有选项,但不要触摸占位符。这个代码适合我。
selector.find('option:not(:first)').remove().trigger('change');
#16
1
I tried the above solutions but it didn't work for me.
我尝试了上述的解决方案,但没有奏效。
This is kind of hack, where you do not have to trigger change.
这是一种技巧,你不需要触发改变。
$("select").select2('destroy').val("").select2();
or
或
$("select").each(function () { //added a each loop here
$(this).select2('destroy').val("").select2();
});
#17
1
as of v.4.0.x...
作为v.4.0.x……
to clear the values, but also restore the placeholder use...
要清除值,还要恢复占位符的使用…
.html('<option></option>'); // defaults to placeholder
if it's empty, it will select the first option item instead which may not be what you want
如果它是空的,它会选择第一个选项项而不是你想要的
.html(''); // defaults to whatever item is first
frankly...Select2 is such a pain in the butt, it amazes me it hasn't been replaced.
坦率地说……选择2是如此的痛苦,它让我惊讶它没有被取代。
#18
1
For the place holder
的占位符
$("#stateID").select2({
placeholder: "Select a State"
});
To reset a select 2
重置选择2
$('#stateID').val('');
$('#stateID').trigger('change');
Hope this helps
希望这有助于
#19
1
This is the correct way:
这是正确的方法:
$("#customers_select").val('').trigger('change');
I am using the latest release of Select2.
我正在使用Select2的最新版本。
#20
0
Before you clear the value using this $("#customers_select").select2("val", "");
Try setting focus to any other control on reset click.
在使用这个$(“#customers_select”)清除值之前。select2(“val”,“”);尝试设置焦点到任何其他控件的复位点击。
This will show the placeholder again.
这将再次显示占位符。
#21
0
The DOM interface, already keeps track of the initial state...
DOM接口,已经跟踪了初始状态…
So doing the following is enough:
因此,做以下的事情就足够了:
$("#reset").on("click", function () {
$('#my_select option').prop('selected', function() {
return this.defaultSelected;
});
});
#22
0
Well whenever I did
每当我做了
$('myselectdropdown').select2('val', '').trigger('change');
I started getting some kind of lag after some three to four triggers. I suppose there's a memory leak. Its not within my code because if I do remove the this line, my app is lag free.
在3到4个触发点之后,我开始有一些延迟。我想可能有内存泄漏。它不在我的代码中,因为如果我删除了这条线,我的应用程序就会被延迟。
Since I have allowClear options set to true, I went with
由于我的allowClear选项设置为true,所以我选择了
$('.select2-selection__clear').trigger('mousedown');
This can be followed by a $('myselectdropdown').select2('close'); event trigger on the select2 dom element in case you wanna close the open suggestion drop down.
然后是$('myselectdropdown').select2('close');在select2 dom元素上的事件触发器,以防您想关闭open suggest下拉。
#23
0
One [very] hacky way to do it (I saw it work for version 4.0.3):
一种[非常]陈腐的方法(我看到它适用于4.0.3版):
var selection = $("#DelegateId").data("select2").selection;
var evt = document.createEvent("UIEvents");
selection._handleClear(evt);
selection.trigger("toggle", {});
- allowClear must be set to true
- allowClear必须设置为true。
- an empty option must exist in the select element
- 选择元素中必须存在一个空选项
#24
0
Using select2 version 3.2 this worked for me:
使用select2版本3.2,这对我有用:
$('#select2').select2("data", "");
Didn't need to implement initSelection
不需要实现initSelection
#25
0
After trying the first 10 solutions here and failing, I found this solution to work (see "nilov commented on Apr 7 • edited" comment down the page):
在尝试了前10个解决方案后,我发现这个解决方案是有效的(参见“nilov对Apr 7•编辑的评论”):
(function ($) {
$.fn.refreshDataSelect2 = function (data) {
this.select2('data', data);
// Update options
var $select = $(this[0]);
var options = data.map(function(item) {
return '<option value="' + item.id + '">' + item.text + '</option>';
});
$select.html(options.join('')).change();
};
})(jQuery);
The change is then made:
然后进行了更改:
var data = [{ id: 1, text: 'some value' }];
$('.js-some-field').refreshDataSelect2(data);
(The author originally showed var $select = $(this[1]);
, which a commenter corrected to var $select = $(this[0]);
, which I show above.)
(作者最初显示的是var $select = $(这个[1]);其中一个评论者更正为var $select = $(这个[0]);
#26
0
My solution is:
我的解决方案是:
$el.off('select2:unselect')
$el.on('select2:unselect', function (event) {
var $option = $('<option value="" selected></option>');
var $selected = $(event.target);
$selected.find('option:selected')
.remove()
.end()
.append($option)
.trigger('change');
});
#27
0
$('myselectdropdown').select2('val', '0')
where 0 is your first value of the dropdown
下拉菜单的第一个值是多少
#28
-1
In order to reset the values and the placeholder I just reinitiate the select2 element:
为了重置值和占位符,我重新启动select2元素:
$( "#select2element" ).select2({
placeholder: '---placeholder---',
allowClear: true,
minimumResultsForSearch: 5,
width:'100%'
});
#1
183
You must define the select2 as
必须将select2定义为
$("#customers_select").select2({
placeholder: "Select a customer",
initSelection: function(element, callback) {
}
});
To reset the select2
重设select2
$("#customers_select").select2("val", "");
#2
92
Select2 has changed their API:
Select2改变了他们的API:
Select2: The
select2("val")
method has been deprecated and will be removed in later Select2 versions. Use $element.val() instead.Select2: Select2(“val”)方法已被废弃,将在以后的Select2版本中删除。使用$ element.val()。
The best way to do this now is:
现在最好的办法是:
$('#your_select_input').val('');
Edit: December 2016 Comments suggest that the below is the updated way to do this:
编辑:2016年12月评论建议以下是更新的方式:
$('#your_select_input').val([]);
#3
67
The accepted answer does not work in my case. I'm trying this, and it's working:
在我的情况下,公认的答案是无效的。我正在尝试这个,它正在起作用:
Define select2:
定义select2:
$("#customers_select").select2({
placeholder: "Select a State",
allowClear: true
});
or
或
$("#customers_select").select2({
placeholder: "Select a State"
});
To reset:
重置:
$("#customers_select").val('').trigger('change')
or
或
$("#customers_select").empty().trigger('change')
#4
19
The only thing can work for me is:
唯一对我有用的是:
$('select2element').val(null).trigger("change")
I'm using version 4.0.3
我用4.0.3版本
#5
14
Select2 uses a specific CSS class, so an easy way to reset it is:
Select2使用特定的CSS类,因此重置它的一种简单方法是:
$('.select2-container').select2('val', '');
And you have the advantage of if you have multiple Select2 at the same form, all them will be reseted with this single command.
如果在同一个表单中有多个Select2,那么所有的选项都将通过这个命令被重新设置。
#6
12
Use this :
用这个:
$('.select').val([]).trigger('change');
#7
7
TO REMOVE SELECTED VALUE
删除选中的值
$('#employee_id').select2('val','');
TO CLEAR OPTION VALUES
清除选项值
$('#employee_id').html('');
#8
5
I know this is kind of an old question, but this works for the select2 version 4.0
我知道这是一个老问题,但这适用于select2版本4.0。
$('#select2-element').val('').trigger('change');
or
或
$('#select2-element').val('').trigger('change.select2');
if you have change events bound to it
如果您有更改事件绑定到它
#9
5
Firstly you must define select2 like this:
首先,您必须这样定义select2:
$('#id').select2({
placeholder: "Select groups...",
allowClear: true,
width: '100%',
})
To reset select2, simply you may use the following code block:
要重置select2,只需使用以下代码块:
$("#id > option").removeAttr("selected");
$("#id").trigger("change");
#10
3
For users loading remote data, this will reset the select2 to the placeholder without firing off ajax. Works with v4.0.3:
对于加载远程数据的用户,这将在不启动ajax的情况下将select2重置为占位符。适用于v4.0.3:
$("#lstProducts").val("").trigger("change.select2");
#11
3
Use following to configure select2
使用以下命令配置select2
$('#selectelementid').select2({
placeholder: "Please select an agent",
allowClear: true // This is for clear get the clear button if wanted
});
And to clear select input programmatically
并以编程方式清除select输入。
$("#selectelementid").val("").trigger("change");
$("#selectelementid").trigger("change");
#12
2
Following the recommended way of doing it. Found in the documentation https://select2.github.io/options.html#my-first-option-is-being-displayed-instead-of-my-placeholder (this seems to be a fairly common issue):
遵循推荐的方法。在文档中发现https://select2.github.io/options。html#my-first- is being-display -instead of- of-my-placeholder(这似乎是一个相当普遍的问题):
When this happens it usually means that you do not have a blank <option></option>
as the first option in your <select>
.
当这种情况发生时,通常意味着您在
as in:
如:
<select>
<option></option>
<option value="foo">your option 1</option>
<option value="bar">your option 2</option>
<option value="etc">...</option>
</select>
#13
2
I was also having this problem and it stems from val(null).trigger("change");
throwing a No select2/compat/inputData
error before the placeholder gets reset. I solved it by catching the error and setting the placeholder directly (along with a width). Note: If you have more than one you will need to catch each one.
我也有这个问题它来自val(null)。trigger(“改变”);在占位符重置之前抛出一个No select2/compat/inputData错误。我通过捕获错误并直接设置占位符(以及宽度)来解决这个问题。注意:如果你有一个以上的,你需要抓住每一个。
try{
$("#sel_item_ID").select2().val(null).trigger("change");
}catch(err){
console.debug(err)
}
try{
$("#sel_location_ID").select2().val(null).trigger("change");
}catch(err){
console.debug(err)
}
$('.select2-search__field')[0].placeholder='All Items';
$('.select2-search__field')[1].placeholder='All Locations';
$('.select2-search__field').width(173);
I'm running Select2 4.0.3
我跑步Select2 4.0.3
#14
2
You can clear te selection by
你可以通过
$('#object').empty();
But it wont turn you back to your placeholder.
但它不会让你回到你的占位符。
So its a half solution
这是一半的解
#15
1
I have tried above solutions but none worked with version 4x.
我已经尝试过以上的解决方案,但是没有一个能与4x版本兼容。
What i want to achieve is: clear all options but don't touch the placeholder. This code works for me.
我想实现的是:清除所有选项,但不要触摸占位符。这个代码适合我。
selector.find('option:not(:first)').remove().trigger('change');
#16
1
I tried the above solutions but it didn't work for me.
我尝试了上述的解决方案,但没有奏效。
This is kind of hack, where you do not have to trigger change.
这是一种技巧,你不需要触发改变。
$("select").select2('destroy').val("").select2();
or
或
$("select").each(function () { //added a each loop here
$(this).select2('destroy').val("").select2();
});
#17
1
as of v.4.0.x...
作为v.4.0.x……
to clear the values, but also restore the placeholder use...
要清除值,还要恢复占位符的使用…
.html('<option></option>'); // defaults to placeholder
if it's empty, it will select the first option item instead which may not be what you want
如果它是空的,它会选择第一个选项项而不是你想要的
.html(''); // defaults to whatever item is first
frankly...Select2 is such a pain in the butt, it amazes me it hasn't been replaced.
坦率地说……选择2是如此的痛苦,它让我惊讶它没有被取代。
#18
1
For the place holder
的占位符
$("#stateID").select2({
placeholder: "Select a State"
});
To reset a select 2
重置选择2
$('#stateID').val('');
$('#stateID').trigger('change');
Hope this helps
希望这有助于
#19
1
This is the correct way:
这是正确的方法:
$("#customers_select").val('').trigger('change');
I am using the latest release of Select2.
我正在使用Select2的最新版本。
#20
0
Before you clear the value using this $("#customers_select").select2("val", "");
Try setting focus to any other control on reset click.
在使用这个$(“#customers_select”)清除值之前。select2(“val”,“”);尝试设置焦点到任何其他控件的复位点击。
This will show the placeholder again.
这将再次显示占位符。
#21
0
The DOM interface, already keeps track of the initial state...
DOM接口,已经跟踪了初始状态…
So doing the following is enough:
因此,做以下的事情就足够了:
$("#reset").on("click", function () {
$('#my_select option').prop('selected', function() {
return this.defaultSelected;
});
});
#22
0
Well whenever I did
每当我做了
$('myselectdropdown').select2('val', '').trigger('change');
I started getting some kind of lag after some three to four triggers. I suppose there's a memory leak. Its not within my code because if I do remove the this line, my app is lag free.
在3到4个触发点之后,我开始有一些延迟。我想可能有内存泄漏。它不在我的代码中,因为如果我删除了这条线,我的应用程序就会被延迟。
Since I have allowClear options set to true, I went with
由于我的allowClear选项设置为true,所以我选择了
$('.select2-selection__clear').trigger('mousedown');
This can be followed by a $('myselectdropdown').select2('close'); event trigger on the select2 dom element in case you wanna close the open suggestion drop down.
然后是$('myselectdropdown').select2('close');在select2 dom元素上的事件触发器,以防您想关闭open suggest下拉。
#23
0
One [very] hacky way to do it (I saw it work for version 4.0.3):
一种[非常]陈腐的方法(我看到它适用于4.0.3版):
var selection = $("#DelegateId").data("select2").selection;
var evt = document.createEvent("UIEvents");
selection._handleClear(evt);
selection.trigger("toggle", {});
- allowClear must be set to true
- allowClear必须设置为true。
- an empty option must exist in the select element
- 选择元素中必须存在一个空选项
#24
0
Using select2 version 3.2 this worked for me:
使用select2版本3.2,这对我有用:
$('#select2').select2("data", "");
Didn't need to implement initSelection
不需要实现initSelection
#25
0
After trying the first 10 solutions here and failing, I found this solution to work (see "nilov commented on Apr 7 • edited" comment down the page):
在尝试了前10个解决方案后,我发现这个解决方案是有效的(参见“nilov对Apr 7•编辑的评论”):
(function ($) {
$.fn.refreshDataSelect2 = function (data) {
this.select2('data', data);
// Update options
var $select = $(this[0]);
var options = data.map(function(item) {
return '<option value="' + item.id + '">' + item.text + '</option>';
});
$select.html(options.join('')).change();
};
})(jQuery);
The change is then made:
然后进行了更改:
var data = [{ id: 1, text: 'some value' }];
$('.js-some-field').refreshDataSelect2(data);
(The author originally showed var $select = $(this[1]);
, which a commenter corrected to var $select = $(this[0]);
, which I show above.)
(作者最初显示的是var $select = $(这个[1]);其中一个评论者更正为var $select = $(这个[0]);
#26
0
My solution is:
我的解决方案是:
$el.off('select2:unselect')
$el.on('select2:unselect', function (event) {
var $option = $('<option value="" selected></option>');
var $selected = $(event.target);
$selected.find('option:selected')
.remove()
.end()
.append($option)
.trigger('change');
});
#27
0
$('myselectdropdown').select2('val', '0')
where 0 is your first value of the dropdown
下拉菜单的第一个值是多少
#28
-1
In order to reset the values and the placeholder I just reinitiate the select2 element:
为了重置值和占位符,我重新启动select2元素:
$( "#select2element" ).select2({
placeholder: '---placeholder---',
allowClear: true,
minimumResultsForSearch: 5,
width:'100%'
});