最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Get the input value after pressing submit button in JavaScript - Stack Overflow

programmeradmin2浏览0评论

I want to get the value of input in JavaScript after pressing submit but the problem is that I get the value I entered in HTML. After I press "Submit Query" the URL change to path/index.html?number=4 if I set the input to 4. But the value printed in console still the same '2'. I want to get the value of 4 not the default value located within HTML file!!

console.log(document.getElementById("number").value)
<input type="number" id="number" name="number" value="2">
<input type="submit" id="submit">

I want to get the value of input in JavaScript after pressing submit but the problem is that I get the value I entered in HTML. After I press "Submit Query" the URL change to path/index.html?number=4 if I set the input to 4. But the value printed in console still the same '2'. I want to get the value of 4 not the default value located within HTML file!!

console.log(document.getElementById("number").value)
<input type="number" id="number" name="number" value="2">
<input type="submit" id="submit">

Share Improve this question edited Jul 7, 2019 at 14:45 Bubbaloo asked Jul 7, 2019 at 14:41 BubbalooBubbaloo 371 gold badge1 silver badge7 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3

You need to add an event listener for your button and then console.log the result.

Try this:

var btn = document.getElementById('submit');
btn.addEventListener('click', func);

function func() {
  console.log(document.getElementById("number").value)
}
<input type="number" id="number" name="number" value="2">
<input type="submit" id="submit">

You have to add and event listener to your submit button so that when you click on it, you get the value of your input each time

document.getElementById("submit").addEventListener("click", function(){
 console.log(document.getElementById("number").value)
});

<input type="number" id="number" name="number" value="2">
<input type="submit" id="submit">
发布评论

评论列表(0)

  1. 暂无评论