[英]Trouble with html input box
I'm making a basic usd converter for practice in html/javascript. 我正在为html / javascript中的实践制作一个基本的usd转换器。 However when I select the euro option it does the same as for the peso option. 但是,当我选择欧元选项时,其作用与比索选项相同。
<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>
You are not comparing select,you are setting it. 您不比较选择,而是对其进行设置。
if (select == 1) {
document.getElementById("answer").innerHTML = ...
} else if (select == 2) {
它应该是select == 1
select=1
将始终返回true,因为这样,您只是在分配一个值,而不检查是否相等
Complete solution here 完整的解决方案在这里
Change your javaScript function as below 如下更改您的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 {
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.