Skip to content

Instantly share code, notes, and snippets.

@mm580486
Created October 10, 2019 14:16
Show Gist options
  • Save mm580486/a8685b19f5d46c58e45b4009c8bea4ff to your computer and use it in GitHub Desktop.
Save mm580486/a8685b19f5d46c58e45b4009c8bea4ff to your computer and use it in GitHub Desktop.
var STATES = JSON.parse($('#states').text());
function initStates() {
states.forEach(function(item, index) {
var newOption = new Option(item.PNAME, item.STATE_CODE, false, false);
$('#states').append(newOption).trigger('change');
})
}
function initCities(id) {
var request = $.ajax({
url: "https://newerror.bankerror.com/api/shop/v1/show-cities",
type: "POST",
cache: false,
data: {
state_code: id
},
dataType: "json"
});
request.done(function(data) {
var json = data;
var filteredObj = json.cities.find(function(item, i) {
console.log(item);
if (item.CODE == id) {
var newOption = new Option(item.PNAME, item.ID, false, false);
$('#cities').append(newOption).trigger('change');
}
$('#cities').prop('disabled', false);
});
});
request.fail(function(jqXHR, textStatus) {
alert('خطایی در ارتباط با سرور رخ داده است' + textStatus);
});
}
function initSelect2() {
$('.select2').select2({
dir: 'rtl',
closeOnSelect: true,
debug: false,
language: 'fa',
multiple: false,
tags: false,
theme: "classic",
scrollAfterSelect: false,
allowClear: false
});
$('#cities').prop('disabled', true);
initStates();
}
initSelect2();
$('#states').on('select2:select', function(e) {
var state = $(this);
initCities($(state).val());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment