繁体   English   中英

一秒钟后使用JS在同一页面上呈现表单数据后数据消失

[英]Data disappears after a split second using JS to render form data on same page

我试图使用JavaScript在同一页面上呈现表单数据。

的JavaScript

function renderInput(age) {
    document.getElementById('display').innerHTML += age;
}

当将提交按钮放置在form元素之外时,它可以工作。

HTML-工作正常

<form>
  <label><b>Enter your age</b></label>
  <input type="text" name="age" id="user_input">
</form>

<input type="submit" 
onclick='renderInput(document.getElementById("user_input").value);'><br/>

<div id='display'>Your age is </div>

当它仅短暂地呈现id为“ display”的div时,如果将提交字段放在form元素内,则消失。

HTML-无效时

<form>
  <label><b>Enter your age</b></label>
  <input type="text" name="age" id="user_input">
  <input type="submit" 
onclick='renderInput(document.getElementById("user_input").value);'><br/>
</form>

<div id='display'>Your age is </div>

我似乎无法解决这个问题。 为什么会这样呢?

您应该使用以下onclick事件返回false来停止提交表单:

<input type="submit" onclick='renderInput(document.getElementById("user_input").value); return false;'>

暂无
暂无

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

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