Json source in multiselect jQuery

I have jQuery multi selection and I want to get the source from json. I took the source code from my autocomplete, which works, but it doesn’t work here.

My code is:

$(document).ready(function () { var warning = $("#message"); $("select").multiselect({ //selectedText: function (numChecked, numTotal, checkedItems) { // return numChecked + ' of ' + numTotal + ' checked'; //}, source: function (request, response) { $.getJSON('http://' + $("[id$='ip']").val() + "/JSON/Auctocomplete.aspx?city=1&term=" + request.term, function (data) { response(data); }); }, select: function (event, ui) { $("#mfr").textContent = ui.item.id; }, selectedList: 5, header: "choose up to 5", click: function (e) { if ($(this).multiselect("widget").find("input:checked").length > 5) { warning.addClass("error").removeClass("success").html("choose up to 5"); return false; } else { warning.addClass("success").removeClass("error").html(""); } } }); }); 
+6
source share
2 answers

I searched and I think there is no source property for jquery multiselect. Take a look at http://www.erichynds.com/blog/jquery-ui-multiselect-widget . Are you sure there is a source property for it?

I advise you to first download select from json and then convert it to multiselect.

 // The empty select element: <select></select> // In javascript: $(document).ready(function () { var url = 'http://...'; $.getJSON(url,function(result){ $.each(result, function(i, field){ var option = $('<option value="' + field.value + '">' + field.text + '</option>'); $('select').append(option); }); $('select').multiselect({...}); }); }); 
+2
source

this is due to the same origin policy . you cannot use ajax to call external sites. if you really want to use, you should use JSONP . Or you can use a server proxy for this. means calling an external site on the server side and making an ajax call to this web service.

for more information see this link

+7
source

Source: https://habr.com/ru/post/943618/


All Articles