jquery select2: Uncaught query function not defined for Select2 tags

前提是你 提交于 2019-12-10 23:07:49

问题


I am using select2 plugin ver 3.5. It is working fine on php file. I am able to select value and able to save them in database.

Scenario

User save the data and after some time he/she wants to update/modify the data. then previously selected value should appear in the form.

I am using Ajax to retrieve the data from database and it is working fine. Issue is i am not able to show selected data in select2 plugin.

I searched on SO and found few relevant questions but i am not able to understand how to use them in my scenario

$("#autofill").change(function() {
        var data1= $('option:selected', this).text();
                        $.ajax({
                    //type: 'post',
                    type: "GET",
                    url:"autofill.php",
                    cache: false,
                   // dataType: 'json',
                    data: 'action1=' + data1, 
                    success: function(data) {                    
                                 data = JSON.parse( data );
                               var keywordArray=data.keywords;
                               $('#tags').select2().select2('val', keywordArray)

But for some reason $('#tags').select2().select2('val', keywordArray) this is not working.

I found this error. Uncaught query function not defined for Select2 tags

Please advise.

Edit: I checked this question and its bit similar to this but not able to sort out issue.

How to set selected value of jquery select2?

I tried

$('#tags').select2('val', {id: 100, text: 'Lorem Ipsum'});
and
$('#tags').select2('val', 'keywordArray')

but no success.

Edit2:

For some reason something is working on.

$('#tags').select2('val', {id: 1, text: 'test'})

This is working but i am getting [object Object]

来源:https://stackoverflow.com/questions/35729864/jquery-select2-uncaught-query-function-not-defined-for-select2-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!