Uso de JavaScript y DOM recta, algo como esto (live example):
var box, oldValue;
// Get a reference to the select box's DOM element.
// This can be any of several ways; below I'll look
// it up by ID.
box = document.getElementById('theSelect');
if (box.addEventListener) {
// DOM2 standard
box.addEventListener("change", changeHandler, false);
}
else if (box.attachEvent) {
// IE fallback
box.attachEvent("onchange", changeHandler);
}
else {
// DOM0 fallback
box.onchange = changeHandler;
}
// Our handler
function changeHandler(event) {
var index, newValue;
// Get the current index
index = this.selectedIndex;
if (index >= 0 && this.options.length > index) {
// Get the new value
newValue = this.options[index].value;
}
// **Your code here**: old value is `oldValue`, new value is `newValue`
// Note that `newValue`` may well be undefined
display("Old value: " + oldValue);
display("New value: " + newValue);
// When done processing the change, remember the old value
oldValue = newValue;
}
(estoy suponiendo que todo lo anterior es dentro de una función, como una función de carga de la página o similar, ya que en la live example, así que no estamos creando innecesarios símbolos globales [box
, oldValue
, 'changeHandler`].)
Tenga en cuenta que el evento change
se eleva por diferentes navegadores en diferentes momentos. Algunos navegadores plantean el evento cuando la selección cambia, otros esperan hasta que el foco abandona el cuadro de selección.
Pero es posible considerar el uso de una biblioteca como jQuery, Prototype, YUI, Closure o any of several others, ya que hacen un montón de estas cosas mucho más fácil.
¿algún código para mostrar? – wajiw