admin管理员组文章数量:1356784
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
2 Answers
Reset to default 3You 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">
本文标签: htmlGet the input value after pressing submit button in JavaScriptStack Overflow
版权声明:本文标题:html - Get the input value after pressing submit button in JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743974032a2570804.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论