我找不到为什么这不起作用。 "的document.getElementById("数")值&#34。赢了"返回"在功能.. 我的HTML代码:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="Currency_Converter.css">
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css"/>
</head>
<body>
<div class="container">
<h1>Currency Converter</h1>
<form id="amount">Dollar amount  $<input id="number" type="number" name="number" onkeyup="getDollarAmount();" onchange="getDollarAmount();" placeholder="type dollar amount here" ></form>
</div>
</body>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type='text/javascript' src="Currency_Converter.js"></script>
</html>
我的JavaScript:
function getDollarAmount() {
var dollarAmount = document.getElementById("number").value;
return (dollarAmount);
}
console.log(getDollarAmount());
我的&#34;函数getDollarAmount()&#34;不会返回表单中的数字。当我&#34; console.log(dollarAmount)&#34;我得到了我的意见。当我拿出&#34; document.getElementById(&#34; number&#34;)。value&#34;并将其替换为数字,(例如:5)&#34; getDollarAmount&#34;函数返回数字(5)。
显然,问题在于&#34; document.getElementById(&#34; number&#34;)。value&#34;因为该功能以其他方式工作。我需要做些什么才能获得&#34; document.getElementById(&#34; number&#34;)。value&#34;返回&#34; getDollarAmount()&#34;?
答案 0 :(得分:0)
尝试
var dollarAmount = $("#number").val();
答案 1 :(得分:0)
这里的错误是你返回的值而不是将dollarAmount变量赋值给输入值属性。
除了在同一个地方返回转换后的货币值的想法,你输入你想要转换的金额不是一个好习惯,而且会让人感到困惑。
您应该有一个输入的地方和一个显示转换值的地方。这是一种更好的用户体验,对您来说更好。
你不需要jQuery。这是一个例子:
var el = document.getElementById("number");
el.addEventListener("keyup", function() {
//You need to assign the value to a variable
var dollarAmount = getDollarAmount();
});
function getDollarAmount() {
return el.value;
}
<input id="number" type="number" name="number" placeholder="type dollar amount here" value="0" >
希望它有所帮助。