使用JavaScript从输入中获取数据 [英] Getting data from input using JavaScript

查看:197
本文介绍了使用JavaScript从输入中获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用JavaScript从< input type ='num'> 中获取值我使用以下代码:

 选择1到5之间的数字< input type ='num'name =input> 

< button id =btn>点击我!< / button>

< script>
var input;

document.getElementById('btn')。onclick = function(){
input = document.getElementById('num');
alert(输入); //检查输入的值是什么
< / script>

这应该得到一个值,但我只是得到一个null,我做错了什么?

解决方案

您的代码存在多个问题



1)您必须关闭函数的括号
它应该是

  document.getElementById('btn')。onclick = function( ){
input = document.getElementById('num');
alert(输入); //检查输出的值
}

2)

  input = document.getElementById('num'); 




getElementById()方法返回ID为$ b的元素带有指定值的$ b属性。


所以ID属性在这里是必不可少的,在你的代码中没有定义ID属性所以你有先定义它

like

 < input type =' number'id =numname =input> 

3) document.getElementById('num'); 不返回输入字段的值
它返回对象
所以如果你想要值然后使用下面的代码

 的document.getElementById( 'NUM')值; 

4)您的输入类型=数字



您可以使用以下代码获得所需的输出

 选择< input type ='number'name =inputid =myid> 

< button id =btn>点击我!< / button>

JS

  var myButton = document.getElementById(btn); 
myButton.onclick = function()
{
alert(document.getElementById(myid).value); // id从哪里来?
}

如果你需要jquery方法,上面的方法是纯JS你可以参考下面

  $(#btn)。click(function(){
var input = $(#myid ).val();
alert(输入)
});


I am trying to get a value out of a <input type='num'> with JavaScript I am using the following code:

Choose a number between 1 and 5 <input type='num' name="input">

<button id="btn">Click me!</button>

<script>
    var input;

    document.getElementById('btn').onclick = function(){
    input = document.getElementById('num');
        alert(input); //To check what value input has
</script>

This should get a value but I just get a null what am I doing wrong?

解决方案

There are more than one problems with your code

1) You have to close the bracket of your function it should be

 document.getElementById('btn').onclick = function(){
    input = document.getElementById('num');
        alert(input); //To check what value is outputted
}  

2)

input = document.getElementById('num');

The getElementById() method returns the element that has the ID attribute with the specified value.

so ID attribute is essential here and in your code there is no ID attribute defined so you have to defined it first

like

<input type='number' id="num" name="input">

3) document.getElementById('num'); does not return the value of input field it returns object so if you want value then use the following code

document.getElementById('num').value;

4) your input type="number"

for the desired output you can use following code

Choose a number between 1 and 5 <input type='number' name="input" id="myid">

<button id="btn">Click me!</button>

JS

    var myButton = document.getElementById("btn");
myButton.onclick = function()
{
         alert(document.getElementById("myid").value); //where does id come from?
}

The above method is pure JS if you need jquery method you can refer below

$( "#btn" ).click(function() {
  var input=$("#myid").val();
    alert(input)
});

这篇关于使用JavaScript从输入中获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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