gpt4 book ai didi

javascript - 关于方程和条件的简单 Html/JavaScript 问题

转载 作者:行者123 更新时间:2023-11-28 01:01:51 24 4
gpt4 key购买 nike

我是编程/编码新手,所以这对你们大多数人来说可能很容易。我应该创建一个用于重量转换的网络应用程序:从美元到日元和日本日元兑美元。假设 1 美元 = 112.57 日元- 使用一个输入框- 使用下拉菜单选择要执行的转换- 这两个公式是:Yen =Dollars*112.57 AND Dollars=Yen/112.57

这是我到目前为止得到的:

<!DOCTYPE html>
<html>
<head>
<script>
function convert()
{
var Dollars, Yen; //declare variables

//INPUT
if (inCurrency.value == "U.S Dollars to Yen") {x.value=Dollars}
else if (inCurrency.value == "Yen to U.S Dollars") {x.value=Yen}

//PROCESSING
Yen = Dollars * 112.57;
Dollars = Yen / 112.57;

//OUTPUT
spYen.innerHTML = Yen;
spDollars.innerHTML = Dollars;

}
</script>
<style>
.CCC {
font-family: Arial;
font-size: 12pt;
color: lightseagreen;
}

.DDD {
font-family: 'Times New Roman', Times, serif;
font-size: 12pt;
color: lawngreen;
}
</style>
</head>
<body>
<h2>Currency Converter</h2>
<span class="CCC">Select one:</span>

<select id="inCurrency">
<option>U.S Dollars to Yen</option>
<option>Yen to U.S Dollars</option>

</select><br />
<span style="color:deeppink">Enter amount </span><span id="x"></span><input
type="text" /><br />

<i>
<span class="DDD">Dollars: </span><span id="spDollars"></span><br />
<span class="DDD">Yen: </span><span id="spYen"></span><br />

</i>

<input type="button" value="Convert Currency" onClick="convert()" />
<br />
<br />
<a href="Index.html">Home

</body>
</html>

This is what it looks like我遇到的问题是头部的实际编码。我不确定如何正确设置它是否执行方程式。我想知道是否有人可以提供帮助。谢谢。

最佳答案

这是获得所需结果的一种方式。我已经为您修复了 HTML 和 Javascript。

直接尝试获取/设置 id 值不是正确的方法。

function convert()
{
var Dollars, Yen; //declare variables

//INPUT
var e = document.getElementById("inCurrency");
var inCurrency = e.options[e.selectedIndex].text;

var amount = document.getElementById("x").value;

//PROCESSING
if (inCurrency == "U.S Dollars to Yen")
{
Dollars = amount;
Yen = Dollars * 112.57;
}
else if (inCurrency == "Yen to U.S Dollars")
{
Yen = amount;
Dollars = Yen / 112.57;
}

//OUTPUT
document.getElementById("spYen").innerHTML = Yen;
document.getElementById("spDollars").innerHTML = Dollars;

}
.CCC {
font-family: Arial;
font-size: 12pt;
color: lightseagreen;
}

.DDD {
font-family: 'Times New Roman', Times, serif;
font-size: 12pt;
color: lawngreen;
}
<h2>Currency Converter</h2>
<span class="CCC">Select one:</span>

<select id="inCurrency">
<option>U.S Dollars to Yen</option>
<option>Yen to U.S Dollars</option>

</select><br />
<span style="color:deeppink">Enter amount </span><input id="x" type="text" /><br />

<i>
<span class="DDD">Dollars: </span><span id="spDollars"></span><br />
<span class="DDD">Yen: </span><span id="spYen"></span><br />

</i>

<input type="button" value="Convert Currency" onClick="convert()" />
<br />
<br />
<a href="Index.html">Home

关于javascript - 关于方程和条件的简单 Html/JavaScript 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52601774/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com