我尝试使用以下方式将select元素的旧值和新值传递给函数:
<select class="browser-default" id="countries" onfocus="this.oldvalue = this.value;" onchange="getCurrency(this);this.oldvalue = this.value;">
<option value="" disabled selected>Choose your country</option>
<option value="1">....</option>
<option value="2">....</option>
</select>
var getCurrency = function(country) {
currency_value = country.value;
country_name = country.options[country.selectedIndex].innerHTML;
old_value = country.oldvalue;
if ( oldvalue === "") {
oldvalue = 10;
}
}
然而,它不起作用。它不会将旧值传递给函数。我在Stackoverflow中看到了类似于另一个问题的东西,并尝试重现它。
答案 0 :(得分:2)
请尝试以下操作。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select class="browser-default" id="countries" onfocus="this.oldvalue = this.value;" onchange="getCurrency(this);this.oldvalue = this.value;">
<option value="" disabled selected>Choose your country</option>
<option value="1">India</option>
<option value="2">Australia</option>
<option value="3">USA</option>
</select>
<script>
var oldValue = $("#countries").val();
var newValue;
$("#countries").change(function(){
newValue=$(this).val();
alert(oldValue + " : " + newValue);
oldValue=newValue;
});
</script>
&#13;