简体   繁体   English

将2个JavaScript变量的内容从一个交换到另一个

[英]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. 基本上,我要执行的操作是获取名为FROM的变量,并将其与名为TO和Vice Visa的变量交换,其原因是允许用户按下按钮,该按钮在按下时会交换变量。 Exactly how it does when you press the swap button on google translate. 当您按Google翻译上的交换按钮时,它的确切功能是什么。 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. 下面是变量ect的代码,但我不知道如何对按钮进行编码,以便它们可以互换使用。

  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. FROMTO变量在这两个函数中是局部的,并且不能同时存在。 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进行交换,因为您需要在更改值之前捕获该值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM