javascript - jQuery Select2 - Select ajax-submitted value -



javascript - jQuery Select2 - Select ajax-submitted value -

i have form submits new entry via ajax , returns entry data. i'm trying returned info automatically selected in select2 field. can id entered input value, i'm not sure how text displayed in span.

here's js have far:

function clientformatresult(client){ var markup = client.first_name + ' ' + client.last_name + ' (' + client.username + ')'; homecoming markup; } function clientformatselection(client) { $('#client-input').empty(); $('#client-input').append('<input type="hidden" name="client" value="' + client.id + '" />'); homecoming client.first_name + ' ' + client.last_name + ' (' + client.username + ')'; } $('#client-selection').select2({ placeholder: 'select client', allowclear: true, minimuminputlength: 1, ajax: { type: 'post', url: 'clients/get_client_list', datatype: 'json', data: function (term) { homecoming {filter: term}; }, results: function (data) { homecoming {results: data}; } }, formatresult: clientformatresult, formatselection: clientformatselection, dropdowncssclass: 'bigdrop', initselection: function (element, callback) { var id = element.val(); if(id != '') { $.ajax('clients/get_client_list/'+id).done(function(data) { info = $.parsejson(data); callback(data); }); } } }); $('#add-client-form').submit(function(e) { e.preventdefault(); var form = $(this), url = form.attr('action'), info = form.serialize(); $.post(url, data, function(data, status, xhr) { $('.form-response').fadeout(400); if(status == 'success') { $('#add-client-modal').modal('hide'); info = $.parsejson(data); $('#client-selection').select2('val', data.client_id); } else { $('#add-client-failure').fadein(400); } }); });

as can see, text displayed meant "john smith (smithj)".

i sorted out, issue info returning. select2 expecting id variable, returning client_id.

javascript php jquery ajax jquery-select2

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -