简体   繁体   English

从输入中获取用户输入的值

[英]To get value of user input from input

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

In my project, I have an input form, and when they add new number and click a button, and new form will appear, and user can put another number, and new input field will continue appearing as long as user clicks button.在我的项目中,我有一个输入表单,当他们添加新数字并单击一个按钮时,会出现新表单,用户可以输入另一个数字,只要用户单击按钮,新输入字段就会继续出现。

What I want is to get value from each input.我想要的是从每个输入中获取价值。 I saw some questions asking similar question, but .document.getElementsByName() didn't work for me.我看到一些问题提出了类似的问题,但 .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');
inputNum = parseInt(userInput.value)

console.log(inputNum)// result is 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。 There is only one input at this moment, because when user clicks add button, new form will be added.此时只有一个输入,因为当用户单击添加按钮时,将添加新表单。

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

      </div>

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

Thank you so much!非常感谢!

I feel the best way for you to handle this is to edit the innerHTML of the div.我觉得你处理这个问题的最好方法是编辑 div 的innerHTML。

for example:例如:

<div id="allTheForms">
    <form action="#" onsubmit="addForm();">
      <input type="text">
      <button type="submit">Submit</button </form>
</div>
<script>
    function addForm() {
    var currentHTML = document.getElementById("allTheForms").innerHTML;
    var newForm = '<form action="#" onsubmit="addForm();"><input type="text"><button type="submit">Submit</button</form>';
    document.getElementById("allTheForms").innerHTML = currentHTML + newForm;
   }
</script>

This should allow you to do what I think you are asking.这应该允许你做我认为你在问的事情。

You could do document.getElementById("money-amount").value to get the amount from the input.您可以执行document.getElementById("money-amount").value从输入中获取金额。 If you are asking for values from multiple inputs you should do.如果您要求来自多个输入的值,您应该这样做。

var userinputs = [];
function getInputs() {
var elem = document.getElementsByClassName("input money");
for (var i = 0; i < elem.length; i++) {
if (typeof elem[i].value !== "undefined") {
userinputs.push(elem[i].value);
}
}
}

Then to access the first amount, you could do userinputs[0] , and so on.然后要访问第一个金额,您可以执行userinputs[0] ,依此类推。

Try this尝试这个

 <input type='text' required id='input' onchange="check()"> <p id='output'>null</p> <!-- the javascript --> <script type='text/javascript'> function check(){ //set the value as a let let input = document.querySelector('#input').value; //show it document.querySelector('#output').textContent = input; } </script>

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

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