【发布时间】:2022-01-07 07:33:53
【问题描述】:
我对使用 Typescript/Javascript 非常陌生,但到目前为止,我在互联网上找到的任何信息都没有帮助我解决我的问题。我正在尝试交换两个选择元素的选项值,以便在 HTML 网站上看到这些元素的交换值,并在 Typescript 文件中交换它们的值以使用它们。
这是我的 HTML 文件中选择元素的样子:
<div class="bigtext">Currency 1:</div>
<div class="smalltext">Your input currency</div>
<select id="select1">
<option></option>
<option>EUR</option>
<option>USD</option>
</select>
<div class="bigtext">Currency 2:</div>
<div class="smalltext">The currency you want to convert to</div>
<select id="select2">
<option></option>
<option>EUR</option>
<option>USD</option>
</select>
这是在表单标签内。应该交换元素值的函数如下所示(在 Javascript 中):
let currency1 = "";
let currency2 = "";
document.getElementById("select1").addEventListener("change", event => {
currency1 = document.getElementById("select1")[0].value;
});
document.getElementById("select2").addEventListener("change", event => {
currency2 = document.getElementById("select2")[0].value;
});
function swapCurrencies() {
let helper = currency1;
let htmlhelper = document.getElementById("select1")[0].value;
currency1 = currency2;
document.getElementById("select1")[0].value = document.getElementById("select2")[0].value;
currency2 = helper;
document.getElementById("select2")[0].value = htmlhelper;
}
这里可能存在很多风格上的不准确之处,但现在我真的很高兴知道为什么它没有按照我想要的方式工作。当我单击 HTML 页面上的按钮时,绝对没有任何反应。
提前致谢!
【问题讨论】:
标签: javascript html typescript select swap