我正在制作基本的usd转换器,用于练习html / javascript。 但是,当我选择欧元选项时,它与peso选项的作用相同。
<html>
<head>
<title>Currency Converter</title>
<style>
</style>
</head>
<body>
<input id="amount"> </input>
<p>usd Contverted to</p>
<p class="output"> </p>
<select id="select"> <option value="1">Peso's</option> <option value="2">Euro's</option> </select>
<p id="answer"> is </p>
<input type="submit" value="Submit" onclick="run()">
<script>
function run() {
var Amount = document.getElementById("amount").value;
if (select = 1) {
document.getElementById("answer").innerHTML = "=-=-= " + Amount * 16.39 + " =-=-=";
} else if (select = 2) {
document.getElementById("answer").innerHTML = "=-=-= " + Amount * 0.9 + " =-=-=";
} else {
}
</script>
</body>
</html>
答案 0 :(得分:1)
您没有比较选择,您正在设置它。
if (select == 1) {
document.getElementById("answer").innerHTML = ...
} else if (select == 2) {
答案 1 :(得分:1)
它应该是select == 1
select=1
将始终返回true,因为这样您只需分配值,而不是检查相等性
答案 2 :(得分:0)
<强> Complete solution here 强>
更改您的javaScript函数,如下所示
function run() {
var Amount=document.getElementById("amount").value;
var select=document.getElementById("select").value;
if (select == 1) {
document.getElementById("answer").innerHTML = "=-=-= " + Amount * 16.39 + " =-=-=";
} else if (select == 2) {
document.getElementById("answer").innerHTML = "=-=-= " + Amount * 0.9 + " =-=-=";
} else {
}
}