[英]Get value from the form by javascript flashes
<body>
search:<input type="text" name="name" id="uniqueID" value="value" />
<button onclick="myFunction()">submit</button>
<p id="surprise"></p>
<script>
function myFunction() {
x = document.getElementById("uniqueID").value;
console.log(x);
}
</script>
</body>
If I put the input field and button inside a <form>
the value appear for second and disappear why is that? 如果我将输入字段和按钮放在
<form>
该值显示为秒,然后消失,为什么呢? ("flashes") (“闪烁”)
By default <button>
in a form acts as <input type="submit">
so whenever you click on the button, the data is posted and the page is reloaded. 默认情况下,表单中的
<button>
充当<input type="submit">
因此,每当您单击按钮时,都会发布数据并重新加载页面。 The reloading makes the value disappear. 重新加载使值消失。
Try this : 尝试这个 :
HTML 的HTML
<form method="POST/GET" action="#" onsubmit="return myFunction()">
.
.
</form>
JAVASCRIPT JAVASCRIPT
function myFunction(){
x = document.getElementById("uniqueID").value;
console.log(x);
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.