首页 > 解决方案 > 如何在两个之间交换选项list 我是 Javascript 新手,我想在单击交换按钮时交换这两个选项 Page HTML <div class="form-group col-4"> <button type="button" class="btn bg-深色文本-白色" id="exchange" style="margin-top: 23px; margin-left: 10px;"> <i class="fa fa-exchange" aria-hidd

问题描述

标签: javascripthtml

解决方案


您需要在 addEventListener 中删除函数调用,它只包含函数名

exchangeButton.addEventListener("click",swap);

function swap() {
  let inp = document.getElementById("inputCurrency").value;
  let out = document.getElementById("outputCurrency").value;
  document.getElementById("inputCurrency").value = out;
  document.getElementById("outputCurrency").value = inp;
}

let amountInput = document.getElementById("amount");

let inputCurrency = document.getElementById("inputCurrency");
let outputCurrency = document.getElementById("outputCurrency");

let convertButton = document.getElementById('convertButton');
//convertButton.addEventListener("click",convertCurrency);

let exchangeButton = document.getElementById("exchange");
exchangeButton.addEventListener("click",swap);
<select id="inputCurrency">
<option value="1">USD</option>
<option value="2">KRW</option>
</select>
<select id="outputCurrency">
<option value="1">USD</option>
<option value="2">KRW</option>
</select>
<input type="text" id="amount"/>
<div class="form-group col-4">
    <button type="button" class="btn bg-dark text-white" id="exchange" style="margin-top: 23px; margin-left: 10px;"> Exchange
          <i class="fa fa-exchange" aria-hidden="true"></i>
    </button>
</div>
<button id="convertButton">Convert</button>


推荐阅读