admin管理员组文章数量:1414605
I'm using Chosen Multiple Select with an "All" option.
Referring to this
Basically what I want to happen is the following:
If the user selects any option other than "All", I want "All" to be automatically unselected - works using this:
if ($('#customTextFilterSelect option[value="ALL"]').attr('selected') == 'selected' && $("#customTextFilterSelect option:selected").length > 1) { $('#customTextFilterSelect option[value="ALL"]').removeAttr("selected"); }
I also want the opposite to work - if the user selects "All", I want other options to be automatically unselected. not sure how to best implement
And lastly, if the user unselects everything (manually, by clicking 'x'), "All" should automatically be selected. kind of working, but the placeholder es back when "All" is selected as if length==0
if ($("#customTextFilterSelect option:selected").length == 0) { $('#customTextFilterSelect option[value="ALL"]').attr('selected', 'selected'); }
I'm using Chosen Multiple Select with an "All" option.
Referring to this
Basically what I want to happen is the following:
If the user selects any option other than "All", I want "All" to be automatically unselected - works using this:
if ($('#customTextFilterSelect option[value="ALL"]').attr('selected') == 'selected' && $("#customTextFilterSelect option:selected").length > 1) { $('#customTextFilterSelect option[value="ALL"]').removeAttr("selected"); }
I also want the opposite to work - if the user selects "All", I want other options to be automatically unselected. not sure how to best implement
And lastly, if the user unselects everything (manually, by clicking 'x'), "All" should automatically be selected. kind of working, but the placeholder es back when "All" is selected as if length==0
if ($("#customTextFilterSelect option:selected").length == 0) { $('#customTextFilterSelect option[value="ALL"]').attr('selected', 'selected'); }
2 Answers
Reset to default 2Here is the solution:
$(function()
{
var cSelect = $('.chzn-select').chosen();
var allItem = cSelect.find("option[value='ALL']"); //reference to the "ALL" option
var rest = cSelect.find("option[value!='ALL']"); //reference for the rest of the options
var allItemAlreadySelected = true; //set a flag for the "ALL" option's previous state
cSelect.change(function(event)
{
if ($(this).find("option:selected").length == 0) //if no selection
{
allItem.prop('selected', true); //select "ALL" option
}
else
{
if (allItem.is(':selected')) //currently "ALL" option is selected, but:
{
if (allItemAlreadySelected == false) //if previously not selected
{
rest.prop('selected', false); //deselect rest
allItem.prop('selected', true); //select "ALL" option
}
else //if "ALL" option is previously selected (already), it means we have selected smthelse
allItem.prop('selected', false); //so deselect "ALL" option
}
}
allItemAlreadySelected = allItem.is(':selected'); //update the flag
$('.chzn-select').trigger("liszt:updated"); //update the control
});
});
Now, you don't need that placeholder at all bec. the control now never gets empty. So, to get rid of the placeholder, all you have to do is; add this attribute to your select
.
data-placeholder=" "
It's value should have a space, otherwise choosen may overwrite it.
<select data-placeholder=" " id="customTextFilterSelect" multiple='multiple' style="width:350px;" class="chzn-select">
Here is the working code on jsFiddle.
Use the following javascript to do this.
$(function () {
//Defining the 'ALL' as default option.
var prevdata = ["ALL"];
$('.chzn-select').chosen().change(function(e) {
if ($(this).find("option:selected").length === 0) {
$(this).find("option[value='ALL']").attr('selected', 'selected');
} else {
var cur_date = $(this).val();
if ($(this).find("option[value='ALL']").attr("selected") == "selected" && $(this).find("option:selected").length > 1)
$(this).find("option[value='ALL']").removeAttr("selected");
if(( $.inArray('ALL', prevdata) == -1) && $.inArray('ALL', cur_date) > -1){
$(this).find('option').removeAttr('selected');
$(this).find("option[value='ALL']").attr("selected", "selected");
}
}
$('.chzn-select').trigger("liszt:updated");
//Storing the current processed value
prevdata = $('#customTextFilterSelect').val();
});
});
Following is the jsFiddle link
http://jsfiddle/qCzK9/7/
本文标签:
版权声明:本文标题:javascript - Chosen Multiple Select - Unselect "All" option when selecting anything else, and vice versa - Sta 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745194864a2647086.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论