如何使用Javascript从下拉列表中获取先前选择的项目和当前选择的项目?

问题描述:

亲爱的先生,

我需要如何获取先前选择的商品和
当前从
选择的项目 Java脚本下拉列表?


注意:
我需要的两个值.如何在javascript中解决此问题????



由MohaN

Dear Sir,

I need, How to get previous selected item and
current selected item from
drop down list using Java script?


Note:
Both of the values i need. How to solve this problem in javascript?????



By MohaN

获取当前选定的项目:
To get the current selected item:
var dropdown = document.getElementById("DropDownListID");
var selValue = dropdown.options[dropdown.selectedIndex].value;



要获得先前的值,可以使用jQuery轻松获得它们:
获取先前选择的下拉项 [在更改之前获取select(下拉列表)的值 [ ^ ].
我不确定使用JavaScript获取先前值的直接方法.但是,如果必须使用JavaScript进行操作,请使用几个隐藏的字段并将先前和当前值存储在其中.更改下拉列表值后,将当前隐藏字段值复制到上一个.隐藏字段值,然后将当前隐藏字段值替换为选定的下拉值.这样,您将拥有两个值.我正在使用隐藏字段,因为我不确定onbeforechange事件是否适用于下拉列表.看看是否可以,这样会容易得多.
另一种方法是尝试将jQuery方法分解为JavaScript. :)



To get the previous value, it''s easy to get them using jQuery:
get previous selected dropdown item[^]
Getting value of select (dropdown) before change[^].
I am not sure of a direct way to get the previous value using JavaScript. But if you have to do it using JavaScript, use couple of hidden fields and store previous and current value in them. When the dropdownlist value is changed, copy current hidden field value to prev. hidden field value and then replace current hidden field value with selected drop down value. In this way you will have both the values. I am using to hidden field because I am not sure if onbeforechange event works for drop down list. Check out if it does, it would be much easier.
The other way would be to try and break the jQuery method to JavaScript. :)


您可以通过以下方式实现

You can achieve this by following way

<script type="text/javascript">
    function GetPriviousValue()
    {
        var currentValue =


(" ).val(); var previousValue =
(".ddlList").val(); var previousValue =