日期:2014-05-16  浏览次数:20409 次

js如何获得下拉菜单改变前的值?
<select name='selectSS' onChange="GaiBian()"><option value='1'>1</option><option value='2'>2</option><option value='3'>3</option></select>

请问在GaiBian()这个方法里我应该怎么样获取改变前的值呀?或者用什么别的事件?

------解决方案--------------------
HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
 <head>
  <script language="JavaScript">
  <!--
    function GaiBian(osel){
        alert(osel.options[osel.selectedIndex].text);
    }
  //-->
  </script>
 </head>

 <body>
  <select name='selectSS' onChange="GaiBian(this)">
  <option value='1'>1 </option> 
  <option value='2'>2 </option> 
  <option value='3'>3 </option> 
  </select> 
 </body>
</html>

------解决方案--------------------
JScript code

var $=function(Id){
    return document.getElementById(Id)?document.getElementById(Id):Id;
};
var $N=function(Name){
    return document.getElementsByName(Name)?document.getElementsByName(Name):Name;
};
//获取select的值
var $S=function(Id){
    return $(Id).options[$(Id).selectedIndex].value;
}
//获取radio的值
var $R=function(name){
    var radio = $N(name);
    var getresult = 0;
    for (var i=0;i<radio.length;i++){
        if(radio[i].checked)
            getresult = radio[i].value;
    }
    return getresult;
};
//调用$S("select的id名")

------解决方案--------------------
HTML code

<html>
<body>
<script type="text/javascript">
var prevVal = null;
window.onload = saveSel;
function $(id){
    return document.getElementById(id);
}
function saveSel(){
    prevVal = $("selectSS").options[$("selectSS").selectedIndex].value;
}
function GaiBian(){
    if(prevVal != null){
        alert("选择之前的值:"+prevVal);
    }
    saveSel();
    alert("现在的值:" + prevVal);
}
</script>
<select name='selectSS' id="selectSS" onChange="GaiBian()"> 
<option value='1'>1 </option> <option value='2'>2 </option> <option value='3'>3 </option> 
</select> 
</body>
</html>

------解决方案--------------------
3楼可行,保存上一项的值
------解决方案--------------------
<script language="JavaScript">
<!--
function GaiBian(obj){
alert(obj.options[obj.selectedIndex].value);
}
//-->
</script>