在不使用jQuery的情况下在JavaScript中获取输入值? [英] Get input value in JavaScript without using jQuery?

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

问题描述

假设我有一个这样的输入字段:

Let's say I have an input field like this:

<input type="text" id="input" value="This is my value">

如何获取属性值?在jQuery中,我可以很容易地得到它:

How can I get the attribute value? In jQuery, I can easily get it with:

$('#input').val() //-> This is my value

我如何使用纯JavaScript这样做?

How do I do so using pure JavaScript?

推荐答案

您可以通过定位ID来获取价值。

其中 inputID 是输入或textarea的 ID

You can get the value by targeting the ID.
Where inputID is the ID of your input or textarea:

document.getElementById('inputID').value;

您也可以通过定位name属性来实现。

其中 inputID 是输入或textarea的名称

You can also do it by targeting the name attribute.
Where inputID is the Name of your input or textarea:

document.getElementsByName('inputID')[0].value;

这篇关于在不使用jQuery的情况下在JavaScript中获取输入值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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