当点击一个按钮时,如何将该数字作为javascript变量存储在html表单中? [英] How would I store the number in an html form input as a javascript variable when a button is clicked?

查看:188
本文介绍了当点击一个按钮时,如何将该数字作为javascript变量存储在html表单中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我想要做的是,当用户在HTML表单输入字段中输入数字,然后按下提交按钮时,数字将存储在一个JavaScript变量中。

 < input type =numbermin =1name =number> 

var number =?

如何使用javascript或jquery进行此操作?



谢谢。

解决方案

这是一个javascript解决方案,只有在jQuery内置到您的浏览器它可能也是,因为它是救世主)

 < form id =form > 
< input id =numbertype =numbermin =1name =number>
<按钮>提交< /按钮>
< / form>

var form = document.getElementById('form');
number = document.getElementById('number');
form.onsubmit = function(){
var variable = number.value;
alert(变量);
};

只需指定提交处理程序,并获取输入的值。 b

我的建议是,尽管如此,请不要下载jQuery,以便您可以存储变量。


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.

<input type="number" min="1" name="number">

var number = ?

How would I do this with javascript or with jquery?

Thank you.

解决方案

Here is a javascript solution, only use this until jQuery comes built into your browser ( it might aswell be, seeing as it's the savior )

<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.

这篇关于当点击一个按钮时,如何将该数字作为javascript变量存储在html表单中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆