JVectormap의 jvectormap-element에서 데이터 속성 (data-code = "US-VA"의 경우 VA)의 마지막 두 문자를 가져 와서이를 사용하려고합니다. 데이터베이스의 State 객체와 비교하고 select2 플러그인에서 해당 카운티를로드합니다. select2의 AJAX에서 데이터를 설정할 수 없습니다. 지금까지 다음 코드를 시도했습니다. 미리 감사드립니다.select2 플러그인에서 JSON 데이터를로드하는 방법
HTML
<input type="hidden" class="tag">
JS : URL에서
$(".jvectormap-element").on("click", function(){
data1 = $(this).data("code");
$state = data1.slice(3, 5);
$(".tag").select2({
tags: true,
multiple: true,
cache: true,
allowClear: true,
ajax: {
type: "get",
url: http_host + '/leads/regions/counties?state=' + $state,
data: {state: $state},
dataType: "json",
processResults: function(data) {
optionsValues='';
for (var i=0;i<data.length;i++) {
optionsValues += '<option>' + data[i].CountyName + '</option>';
}
}
}
}); console.log($(".tag").select2('data'));
});
});
json으로 데이터 :
[{"CountyName":"Accomack"},{"CountyName":"Albemarle"},{"CountyName":"Alexandria City"},.....]