Skip to content

Instantly share code, notes, and snippets.

@dotkebi
Created August 3, 2016 10:32
Show Gist options
  • Save dotkebi/dce291358c55237d80e12fa7c365aff3 to your computer and use it in GitHub Desktop.
Save dotkebi/dce291358c55237d80e12fa7c365aff3 to your computer and use it in GitHub Desktop.
javascript select options by ajax
<script>
function onCodeChanged() {
var select = document.getElementById('category');
var value = select.options[select.selectedIndex].value;
var text = select.options[select.selectedIndex].text + '선택';
$.ajax({
type: "POST"
, url: "url"
, data: "category=" + value
, success: function (response) {
var optionToAdd = function (code, label) {
var opt = document.createElement('option')
opt.value = code
opt.text = label
return opt
}
var code = document.getElementById('code');
while (code.options.length > 0) {
code.remove(0);
}
code.add(optionToAdd(null, text), null)
response.forEach(
function (value, index, ar) {
code.add(optionToAdd(value.code, value.label), null)
}
)
code = null
optionToAdd = null
response = null
}
});
select = null
value = null
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment