Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/375.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
表单输入未保存在JavaScript函数中。为什么?_Javascript_User Input - Fatal编程技术网

表单输入未保存在JavaScript函数中。为什么?

表单输入未保存在JavaScript函数中。为什么?,javascript,user-input,Javascript,User Input,我不明白这为什么不起作用。“document.getElementById(“number”).value”不会在函数中“返回”。。 我的HTML代码: <!DOCTYPE html> <html> <head> <link rel="stylesheet" type="text/css" href="Currency_Converter.css"> <link rel="stylesheet" href="http:/

我不明白这为什么不起作用。“document.getElementById(“number”).value”不会在函数中“返回”。。 我的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 &ensp;$<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>
我的“函数getDollarAmount()”不返回放入表单中的数字。当我输入“console.log(dollarAmount)”时,我得到了我的输入。当我取出“document.getElementById(“number”).value”并用一个数字替换它时,(例如:5),“getDollarAmount”函数返回数字(5)

显然,问题在于“document.getElementById(“number”).value”,因为函数以其他方式工作。要将“document.getElementById(“number”).value”返回到“getDollarAmount()”,我需要做什么

试试看

var dollarAmount = $("#number").val();

这里的错误是返回值,而不是将dollarAmount变量指定给输入值属性

除此之外,在键入要转换为的金额的同一位置返回转换后的货币值的想法不是一个好的做法,而且会令人困惑

您应该有一个输入位置和一个显示转换值的位置。这是一种更好的用户体验,对您更有利

您不需要jQuery。下面是一个例子:

var el=document.getElementById(“编号”);
el.addEventListener(“键控”,函数(){
//您需要将该值赋给一个变量
var dollarAmount=getDollarAmount();
});
函数getDollarAmount(){
返回el.value;
}

是否尝试在JQuery样式中获取元素
$(“#number”)
console.log
该值如果在字段中输入数字,则运行
console.log(getDollarAmount())在控制台中,它打印什么?您是否检查了JS是否正在执行该函数?是。如果我将“return”改为“console.log(dollarAmount)”,控制台将显示键入的数字。JS正在执行该函数。console.log(getDollarAmount())为空,但executed@Carcigenicate他在HTMLTank you中导入了jquery,但是jquery的性能与JS完全一样。谢谢。然而,我只是试图捕获这个函数中的输入。不包括JS,它带来了API货币数据。我需要捕获dollarAmount输入,以便将其乘以API数据。使用getDollarAmount,我只需要捕获dollarAmount输入。不客气。您可以使用我的示例为“change”事件创建一个eventListener,并从中调用getDollaAramount()函数。在getDollarAmount()函数中,只需返回el.value即可。这样getDollarAmount将返回输入的值。您能告诉我如何让getDollarAmount返回dollarAmount输入吗?有了提供的功能,我如何更改它以显示用户输入?稍后我将乘以货币数据。“函数getDollarAmount(){var dollarAmount=document.getElementById(“number”).value;返回(dollarAmount);}console.log(getDollarAmount());”我为您简化了HTML。谢谢,我帮你修改了密码。该函数返回输入值,但如果要对其执行操作,则需要将其指定给变量。
var dollarAmount = $("#number").val();