I try to implement following scenario in select2 multiple select.
Here is my html:
<select multiple="multiple" name="transfer_posting[pap_facility_feature_ids][]" id="transfer_posting_pap_facility_feature_ids"><option value="No Required Features">No Required Features</option>
<option value="13">Pool</option>
<option value="15">Sauna</option>
<option value="16">Salon</option>
<option value="17">Massage</option>
</select>
and javascript:
$('#transfer_posting_pap_facility_feature_ids').select2({
placeholder: 'Select Feature(s)',
allowClear: true
});
$('#transfer_posting_pap_facility_feature_ids').on("select2:selecting", function(e) {
var selectedFeature = e.params.args.data.text
console.log(selectedFeature);
if (selectedFeature === 'No Required Features') {
$('#transfer_posting_pap_facility_feature_ids').val(null).trigger("change");
$('#transfer_posting_pap_facility_feature_ids').select2({
placeholder: 'No Required Features'
});
}
});
Here is the link to js fiddle: http://jsfiddle.net/mxqc76e8/3/
How should I update my code to make this scenario works?
Can you please check the updated fiddle code. Updated Fiddle
Added some condition with code changes a bit
Hope it will help you
$('#transfer_posting_pap_facility_feature_ids').select2({
placeholder: 'Select Feature(s)',
allowClear: true
});
$('#transfer_posting_pap_facility_feature_ids').on("select2:selecting", function(e) {
if ($("#transfer_posting_pap_facility_feature_ids option[value='No Required Features']:selected").length > 0){
alert('No Required Feture is selected');
exit;
}
var selectedFeature = e.params.args.data.text
if (selectedFeature =='No Required Features') {
$('#transfer_posting_pap_facility_feature_ids').select2({
placeholder: 'No Required Features'
});
$('li.select2-selection__choice').remove();
$('#transfer_posting_pap_facility_feature_ids').val('');
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.