Created
October 10, 2019 14:16
-
-
Save mm580486/a8685b19f5d46c58e45b4009c8bea4ff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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