我是Java语言的新手,我想在单击交换按钮Page
时交换这两个选项HTML
<div class="form-group col-4">
<button type="button" class="btn bg-dark text-white" id="exchange" style="margin-top: 23px; margin-left: 10px;">
<i class="fa fa-exchange" aria-hidden="true"></i>
</button>
</div>
JavaScript
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());
答案 0 :(得分:1)
您需要在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>
答案 1 :(得分:0)
addEventListener
需要一个用于第二个参数的函数。添加一个调用swap()
函数的回调函数。
exchangeButton.addEventListener("click", e => swap());