简体   繁体   中英

Swap the contents of 2 JavaScript variables from one to the other

Basically what I am wanting to do is take the variable named FROM and swap it with the variable named TO and vice visa, the reason for this is to allow the user to press a button which swaps the variables over when pressed. Exactly how it does when you press the swap button on google translate. Below is the code for the variables ect, but I have no idea on how to code the button so they swap over so to speak.

  function save_options_from() {
  var select = document.getElementById("FROM");
  var FROM = select.children[select.selectedIndex].value;
  localStorage["default_currency"] = FROM;

var
}

  function save_options_to() {
  var select = document.getElementById("TO");
  var TO = select.children[select.selectedIndex].value;
  localStorage["default_currency_to"] = TO;

The FROM and TO variables are local to the two functions and don't exist at the same time. I think what you want is this:

var originalDefault = localStorage['default_currency'];
localStorage['default_currency'] = localStorage['default_currency_to'];
localStorage['default_currency_to'] = originalDefault;

我建议使用两个隐藏字段FROM_old和TO_old进行交换,因为您需要在更改值之前捕获该值。

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