简体   繁体   中英

how to update second select based on first select box in chosen prototype javascript?

i am using chosen prototype. i want to update second select box based on first select box. how to do this? i can able to fire onchange event but i am not able to update the second select box based on onchange event in first select box.

<select class="chzn-select" id="one">
    <option>one</option>
    <option>two</option>
    <option>three</option>
</select>
<select class="chzn-select" id="two">
    </select>


$$('#one').each(function(select) {
    new Chosen(select);
})
.invoke('observe', 'change', function() {
     alert(document.getElementById("one").value);

    //update the second select here


});
$$('#two').each(function(select) {
    new Chosen(select);
})
.invoke('observe', 'change', function() {
    alert(document.getElementById("two").value);
});

Here is example update code based on DOM:

// get value of #one
var v = document.getElementById("one").value;

// get select #two
var sel = document.getElementById("two");

// remove all options
while (sel.childNodes.length > 0) {
    sel.removeChild(sel.childNodes[0]);
}

// add 2 new options
var o1 = document.createElement('option');
o1.innerHTML = v + '-one';
sel.appendChild(o1);

var o2 = document.createElement('option');
o2.innerHTML = v + '-two';
sel.appendChild(o2);

// update chosen
Event.fire($("two"), "liszt:updated");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM