prototype 을 이용해 select box 채우기
간단한 예이다.
우선 ajax로 요청한 결과이다.(json type이다)
getAllRecords.htm?view=json
[code js]
{
records:[
{ key1: "val11", key2 : "val21"},
{ key1: "val12", key2 : "val22"},
{ key1: "val13", key2 : "val23"},
{ key1: "val14", key2 : "val24"},
{ key1: "val15", key2 : "val25"}
]
}
[/code]
getItems.htm?view=json
[code js]
{
records:[
{ key3:"val31"},
{ key3:"val31"},
{ key3:"val31"},
{ key3:"val31"}
]
}
[/code]
js파일 : foo.js
[code js]
function foo1(){
new Ajax.Request('getAllRecodrds.htm?view=json',{
method : 'get',
onSuccess: function (transport) {
var mysel = $('mysel');
var res = transport.responseText.evalJSON();
res.records.each(function(r){
mysel.appendChild(
new Element('option',{'value':r.key2}).update(r.key1+' '+r.key2)
)});
}
);
}
function foo2(){
new Ajax.Request('getItems.htm?view=json',{
method : 'get',
parameters : {key2: $('mysel').value},
onSuccess: function (transport) {
var mysel2 = $('mysel2');
var res = transport.responseText.evalJSON();
mysel2.childElements().each(function(r){ r.remove()});
res.records.each(function(r){
mysel2.appendChild(
new Element('option',{'value':r.key3}).update(r.key3)
)});
}
);
}
[/code]
foot.html
[code html]
<html>
<head>
<script type="text/javascript" src="prototype/prototype.js"></script>
<script type="text/javascript" src="foo.js"></script>
<script type="text/javascript">
document.observe("dom:loaded", function(){
foo1();
});
</script>
</head>
<body>
선택 : <select id="mysel" onchange="foo2()"></select>
아이템 : <select id="mysel2"></select>
</body>
</html>
[/code]
댓글
댓글 쓰기