迹忆客 专注技术分享

当前位置:主页 > 学无止境 > WEB前端 > JavaScript >

如何在 JavaScript 中获取输入值

作者:迹忆客 最近更新:2024/03/20 浏览次数:

在 JavaScript 中,我们可以通过选择 DOM 输入元素并使用 value 属性来获取输入值,而不需要将其包裹在表单元素中。

JavaScript 有不同的方法来选择 DOM 输入元素。下面的每个方法都有一个代码示例,你可以在你的机器上运行。


使用 document.getElementById(id_name) 来获取 JavaScript 中的输入值

我们给 DOM 输入元素赋予 id 属性,然后用 document.getElementById(id_name) 来选择 DOM 输入元素,你可以直接使用 value 属性。

例子

<!DOCTYPE html> 
<html> 
  
<body> 
  
  <h4> 
      Change the text of the text field  
      ,and then click the button. 
  </h4> 
  
  <label for="domTextElement">Name: </label>
  <input type="text" id="domTextElement" > 
  
  <button type="button"  onclick="getValueInput()"> 
      click me!! 
  </button> 
  
  <p id="valueInput"></p> 

  <script> 

    const getValueInput = () =>{
      let inputValue = document.getElementById("domTextElement").value; 
      document.getElementById("valueInput").innerHTML = inputValue; 
    }
    
  </script> 
</body> 
  
</html> 

在 JavaScript 中使用 document.getElementsByClassName('class_name') 来获取输入值

我们给 DOM 输入元素赋予 class 属性,然后用 document.getElementsByClassName('class_name') 来选择 DOM 输入元素,但是如果我们有不同的 DOM 输入元素有相同的 class 名称,那么它将返回一个 DOM 输入数组,所以我们应该通过给出索引号来指定选择哪一个:document.getElementsByClassName('class_name')[index_number].value

例子

<!DOCTYPE html> 
<html> 
  
<body> 
  
  <h4> 
      Change the text of the text field  
      ,and then click the button. 
  </h4> 
  
 <input type="text" class="domTextElement" > 
  
  <label for="domTextElement">Name: </label>
  <input type="text" class="domTextElement" > 
  
  <button type="button"  onclick="getValueInput()"> 
      click me!! 
  </button> 
  
  <p id="valueInput"></p> 

  <script> 

    const getValueInput = () =>{
      let inputValue = document.getElementsByClassName("domTextElement")[1].value; 
      document.getElementById("valueInput").innerHTML = inputValue; 
    }
    
  </script> 
</body> 
  
</html> 

使用 document.querySelector('selector') 来获取 JavaScript 中的输入值

document.querySelector('selector') 使用 CSS 选择器,也就是说,它可以通过 DOM 元素的 id、class、tag 名和 name 属性来选择元素。

例子: document.querySelector('selector')

document.querySelector('class_name')
document.querySelector('id_name')
document.querySelector('input')                    // tag name
document.querySelector('[name="domTextElement"]')  // name property

例子:

<!DOCTYPE html> 
<html> 
  
<body> 
  
  <h4> 
      Change the text of the text field  
      ,and then click the button. 
  </h4> 
  
 <input type="text" id="domTextElement1" > 
  
  <label for="domTextElement">Name: </label>
  <input type="text" class="domTextElement2" > 
  
  <button type="button"  onclick="getValueInput()"> 
      click me!! 
  </button> 
  
  <p id="valueInput"></p> 

  <script> 

    const getValueInput = () =>{
      let inputValue1 = document.querySelector("#domTextElement1").value; 
      let inputValue2 = document.querySelector(".domTextElement2").value; 
      document.querySelector("#valueInput").innerHTML = `First input value: ${inputValue1} Second Input Value: ${inputValue2}`; 
    }
    
  </script> 
</body> 
  
</html> 

转载请发邮件至 1244347461@qq.com 进行申请,经作者同意之后,转载请以链接形式注明出处

本文地址:

相关文章

Do you understand JavaScript closures?

发布时间:2025/02/21 浏览次数:108 分类:JavaScript

The function of a closure can be inferred from its name, suggesting that it is related to the concept of scope. A closure itself is a core concept in JavaScript, and being a core concept, it is naturally also a difficult one.

Do you know about the hidden traps in variables in JavaScript?

发布时间:2025/02/21 浏览次数:178 分类:JavaScript

Whether you're just starting to learn JavaScript or have been using it for a long time, I believe you'll encounter some traps related to JavaScript variable scope. The goal is to identify these traps before you fall into them, in order to av

JavaScript POST

发布时间:2024/03/23 浏览次数:96 分类:JavaScript

本教程讲解如何在不使用 JavaScript 表单的情况下发送 POST 数据。

扫一扫阅读全部技术教程

社交账号
  • https://www.github.com/onmpw
  • qq:1244347461

最新推荐

教程更新

热门标签

扫码一下
查看教程更方便