Created
August 3, 2016 10:32
-
-
Save dotkebi/dce291358c55237d80e12fa7c365aff3 to your computer and use it in GitHub Desktop.
javascript select options by ajax
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
<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