0

I use select2 with ajax search.How to set some option couldn't be selected. for example,like the image below enter image description here

I wrote a 'w' in the input, but the first line in the drop down is not the data come from ajax search. how to set it couldn't be selected or don't show it?

html code

<select id="normalSymptom" name="normalSymptom" class="form-control select2-multiple" multiple > </select>

javascript code

$('#normalSymptom').select2({
    tags: true,
    multiple: true,
    tokenSeparators: [','],
    minimumInputLength: 1,
    minimumResultsForSearch: 10,
    ajax: {
        url: "/DiseaseSearch/symptomSearch",
        dataType: "json",
        type: "GET",
        data: function (params) {
            var queryParameters = {
                word: params.term
            }
            return queryParameters;
        },
        processResults: function (data) {
            return {
                results: $.map(data, function (item) {
                    return {
                        text: item.name,
                        id: item.id
                    }
                })
            };
        }
    }

});
5
  • Please provide your js code
    – AsgarAli
    Nov 2, 2016 at 11:48
  • thank you , here is the code Nov 2, 2016 at 11:54
  • Does this white tag come from ajax ?
    – AsgarAli
    Nov 2, 2016 at 11:55
  • yes white tag come form ajax. but w is what I input Nov 2, 2016 at 12:09
  • So any tag that starts with w should not be selected. Right ?
    – AsgarAli
    Nov 2, 2016 at 12:19

4 Answers 4

3

By default you are allowed to create new tags.

To disable tag creation, supply a createTag option. This option is a function that indicates whether a given tag is allowable. In your case, you want to allow no tags to be created, so return null, e.g.,

$('#normalSymptom').select2({
  tags: true,
  multiple: true,
  createTag: function (params) {
    return null;
  },
  // etc.
});

See JS Fiddle

0
0

I've not played around using select2 for a while, but i believe that is the intended behavior of the plugin. This being said, You should be able to find the id/class of the element that has the 'w' in and set it to display: none;, this has worked for me in the past.

0

try this

$('select').select2({tags:false});
2
  • That's only an option if they don't want tags, which it looks like they do, based on setting tags to true :/ Nov 2, 2016 at 11:56
  • Oh , actually, I need tags:ture, but I just don't want the first line that I input show up Nov 2, 2016 at 11:57
0

Remove "multiple" from your html

<select multiple></select>

Actually whenever we write multiple in select it become tags to select more than 1 option. When you remove it, it become like single select option. I Hope it is helpful! :)

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.