单击按钮时,如何将数字存储在 html 表单输入中作为 javascript 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18157411/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How would I store the number in an html form input as a javascript variable when a button is clicked?
提问by jdc987
Basically what I want to do is when the user types a number in to an HTML form input field then presses the submit button, the number is stored in a javascript variable.
基本上我想要做的是当用户在 HTML 表单输入字段中键入一个数字然后按下提交按钮时,该数字存储在一个 javascript 变量中。
<input type="number" min="1" name="number">
var number = ?
How would I do this with javascript or with jquery?
我将如何使用 javascript 或 jquery 做到这一点?
Thank you.
谢谢你。
回答by
A simple javascript solution is to do this.
一个简单的 javascript 解决方案就是这样做。
var number = document.getElementById("number").value;
This saves the value of the text box under the name 'number'. If you know javascript you can then use it to do pretty much anything. Hope this helps.
这会将文本框的值保存在名称“数字”下。如果您了解 javascript,那么您几乎可以使用它来做任何事情。希望这可以帮助。
Please note that to do this, you need to use id="number"
, instead of name="number"
.
请注意,要执行此操作,您需要使用id="number"
, 而不是name="number"
。
Complete code could be something like
完整的代码可能类似于
<input type="number" id="number" onchange="save()">
this means that as soon as the user inputs a number, the save()function will be called in the javascript file.
这意味着只要用户输入一个数字,就会在 javascript 文件中调用save()函数。
javascript
javascript
function save() {
var number;
number = document.getElementById("number").value;
// Do whatever you want with the value here.
}
回答by iConnor
Here is a javascript solution, only use this until jQuery comes built into your browser ( it might aswell be, seeing as it's thesavior )
这是一个 javascript 解决方案,只有在 jQuery 内置到您的浏览器中之前才使用它(它也可能是,因为它是救世主)
<form id="form">
<input id="number" type="number" min="1" name="number">
<button>Submit</button>
</form>
var form = document.getElementById('form');
number = document.getElementById('number');
form.onsubmit = function() {
var variable = number.value;
alert( variable );
};
Just assign a submit handler, and get the value of the input.
只需分配一个提交处理程序,并获取输入的值。
My advice is to you though, please do not download jQuery just so you can store a variable.
不过,我的建议是,请不要下载 jQuery,只是为了存储变量。
回答by Erik5388
Here's your html,
这是你的html,
<form id="form">
<input type="number" min="1" name="number" />
<input type="submit" value="submit">
</form>
With jQuery you can do this,
使用 jQuery,你可以做到这一点,
var number = 0;
var $form = $("#form");
var $input = $form.find("input[name=number]");
With some events you can add the value of the input to the number variable,
对于某些事件,您可以将输入的值添加到数字变量中,
$form.submit(function(e){
e.preventDefault();
number = +($input.val());
/*
takes value from input when the form is submitted.
This will work even when you hit "enter" on your keyboard.
*/
});
In summary,
总之,
<form id="form">
<input type="number" min="1" name="number" />
<input type="submit" value="submit">
</form>
<script>
var number = 0;
var $form = $("#form");
var $input = $form.find("input[name=number]");
$form.submit(function(e){
e.preventDefault();
number = +($input.val());
/*
takes value from input when the form is submitted.
This will work even when you hit "enter" on your keyboard.
*/
alert(number);
});
</script>
Hope that helps!
希望有帮助!