jquery - how manually set a vaule in a select2 which is fed from ajax call - laravel -
todo
i trying set select2 value value fed ajax reqest.
i have tried answers in these 2 threads: one two.
my code:
html:
// blade {!! form::select('author_id', $author_array, @$auth_preset, array('class'=>'', 'id' => 'select_the_author', 'get-byajax' => 'author', 'ajax-target' => '#author_placeholder')) !!}
// renders html (befere select2 initialization):
(select author...)
my js:
$("select[get-byajax]").each(function(){ var baseurl = "{{url::to('get_by_ajax/')}}"; var itemkind = $(this).attr("get-byajax"); var readyurl = baseurl + '/' + itemkind; $(this).select2({ ajax: { url: readyurl, datatype: 'json', delay: 250, data: function (params) { return { q: params.term }; }, processresults: function (data) { return { results: data }; }, cache: true }, escapemarkup: function (markup) {return markup; }, // let our custom formatter work minimuminputlength: 2 }); });
what have tested:
<a data-set-unknown-author="" class='btn btn-info btn-sm' data-puthere="#new_author" title='nowy autor' data-variation="large"><i class="plus icon"></i><i class="fa fa-plus icon"></i> set select value 24</a>
$('body').on('click', '[data-set-unknown-author]', function() { console.log('author 24 jquery works'); // commented commands below don't work: // $("#select_the_author").select2().select2('val','24'); // $("#select_the_author").val("24").trigger("change"); // $("#select_the_author").val("24"); // $("#select_the_author").select2("data", { id: 24, text: "unknown" }); // $('#select_the_author').select2('data', {id: 24, a_key: 'unknown'}); // $("#select_the_author").select2('data', { id:"24", text: "unknown"}); // $("#select_the_author").select2().select2('val','24'); // $("#select_the_author").select2("val", "24").trigger("change"); });
Comments
Post a Comment