简体   繁体   English

获取值表单输入

[英]Get value form input

I am learning Javascript and trying to get user input value from input.我正在学习 Javascript 并试图从输入中获取用户输入值。

In my project, I have a few inputs form and want to get the number value from each input.在我的项目中,我有几个输入表单,想从每个输入中获取数值。 I saw some questions asking similar stuff, and I tried some DOM but none of them worked to my code.我看到一些问题提出了类似的问题,我尝试了一些 DOM,但没有一个对我的代码有效。

I tried document.getElementsByName()我试过 document.getElementsByName()

let userInput =      document.getElementsByName('moneyamount');
inputNum = parseInt(userInput.value);

console.log(inputNum); //result is NaN

I also tried document.querySelector()我也试过 document.querySelector()

let userInput =     document.querySelector('#money-amount');
inputNum = parseInt(userInput.value);

console.log(inputNum) 

which worked perfect, but I need to get value from a few inputs, so querySelector will not be good for this situation.效果很好,但我需要从一些输入中获取价值,因此 querySelector 不适用于这种情况。 so I tried document.querySelectorAll() as well, however,所以我也尝试了 document.querySelectorAll() ,但是,

let userInput = document.querySelectorAll('#money-amount');让 userInput = document.querySelectorAll('#money-amount'); inputNum = parseInt(userInput.value) inputNum = parseInt(userInput.value)

console.log(inputNum)// result is NaN console.log(inputNum)// 结果是 NaN

I am not sure what DOM I should use in this situation.我不确定在这种情况下应该使用什么 DOM。 I need to select all elements and need to get value from them.我需要选择所有元素并需要从中获取价值。

This is my HTML of input.这是我的输入 HTML。

      <input
        class="input money"
        type="number"
        id="money-amount"
        placeholder="Enter amount"
        name="moneyamount"
        required
      />
      <input
        class="input money"
        type="number"
        id="money-amount1"
        placeholder="Enter amount"
        name="moneyamount"
        required
      />
      <input
        class="input money"
        type="number"
        id="money-amount2"
        placeholder="Enter amount"
        name="moneyamount"
        required
      />

I hope you guys could help this problem.我希望你们能帮助解决这个问题。

Thank you so much!非常感谢!

document.getElementsByName doesnot mean use the name attribute of input HTML element. document.getElementsByName并不意味着使用输入 HTML 元素的 name 属性。

It mean use it by input name like this:这意味着通过输入名称使用它,如下所示:

 let userInput = document.getElementsByName('input');

But its not of your use because it will all the three elements, You can use document.getElementById.但它不是你的用途,因为它会所有三个元素,你可以使用document.getElementById. Please find the example below:请在下面找到示例:

let userInput = document.getElementsByName('money-amount');
inputNum = parseInt(userInput.value);

console.log(inputNum) 

That is because document.querySelectorAll() returns a NodeList.那是因为document.querySelectorAll()返回一个 NodeList。 That means that you need to iterate through the list before attempting to access the value of individual elements.这意味着您需要在尝试访问单个元素的值之前遍历列表。 Since all your input elements have the class input money , then you can use the selector to retrieve all their values:由于您的所有输入元素都具有类input money ,因此您可以使用选择器来检索它们的所有值:

let userInputs = document.querySelectorAll('.input.money');

userInputs.forEach(userInput => {
  console.log(parseInt(userInput.value));
});

To collect all their values, you will likely have to map them to an array, for which you can do this:要收集它们的所有值,您可能必须将它们映射到一个数组,您可以这样做:

let userInputs = document.querySelectorAll('.input.money');

let values = Array.from(userInputs).map(userInput => parseINt(userInput.value));
console.log(values);

See proof-of-concept example:请参阅概念验证示例:

 document.querySelector('#btn').addEventListener('click', () => { let userInputs = document.querySelectorAll('.input.money'); userInputs.forEach(userInput => { console.log(parseInt(userInput.value)); }); let values = Array.from(userInputs).map(userInput => parseInt(userInput.value)); console.log(values); });
 <input class="input money" type="number" id="money-amount" placeholder="Enter amount" name="moneyamount" required value="123" /> <input class="input money" type="number" id="money-amount1" placeholder="Enter amount" name="moneyamount" required value="456" /> <input class="input money" type="number" id="money-amount2" placeholder="Enter amount" name="moneyamount" required value="789" /> <button type="button" id="btn">Get money amounts</button>

Whenever you want to access multiple-element with one query you should try to use the class not id or name每当你想用一个查询访问多元素时,你应该尝试使用class而不是idname

id & name should be unique in each page idname在每个页面中应该是唯一的

 var allMoney = document.getElementsByClassName("money"); console.log(allMoney[0].value); console.log(allMoney[1].value); console.log(allMoney[2].value);
 <input class="input money" type="number" id="money-amount" placeholder="Enter amount" name="moneyamount" value="1" required /> <input class="input money" type="number" id="money-amount1" placeholder="Enter amount" name="moneyamount" value="2" required /> <input class="input money" type="number" id="money-amount2" placeholder="Enter amount" name="moneyamount" value="3" required />

document.querySelectorAll() returns a NodeList. document.querySelectorAll()返回一个 NodeList。 When you use document.querySelectorAll('input') , all three nodes will return in a NodeList format:当您使用document.querySelectorAll('input') ,所有三个节点都将以 NodeList 格式返回:

[input#money-amount.input.money, input.#money-amount1.input.money, input#money-amount2.input.money]

Where # represents the id of the element and .其中 # 代表元素的 id, . represents the class of the element.表示元素的类。 The individual elements can then be accessed by using their indexes.然后可以使用它们的索引访问各个元素。 ie document.querySeletorAll('input')[0] would give you the first element in the NodeList, document.querySeletorAll('input')[1] would give you the second element, etc. The indexes can also be defined as constants so its much easier to read than using 0,1,2.document.querySeletorAll('input')[0]会给你 NodeList 中的第一个元素, document.querySeletorAll('input')[1]会给你第二个元素,等等。索引也可以定义为常量所以它比使用 0,1,2 更容易阅读。
For example, const cost = 0, document.querySelectorAll('input')[cost] .例如, const cost = 0, document.querySelectorAll('input')[cost]

You can test the commands out within the console of the Chrome Developer tools.您可以在 Chrome 开发人员工具的控制台中测试这些命令。 This will give you a better visual of what elements you're selecting and what values are returning by calling different methods.这将使您更好地了解正在选择的元素以及通过调用不同方法返回的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM