gpt4 book ai didi

javascript - 来自同一个按钮的多次打印? html

转载 作者:行者123 更新时间:2023-11-28 11:29:46 25 4
gpt4 key购买 nike

我有输入字段和提交按钮,单击按钮会在网页上显示输入。当我输入一条新消息时,它只显示此消息,并删除旧消息。我怎样才能单击按钮并仍然保留旧消息和新消息?

function outputdisplayname(){
var x, y, z;
x = document.getElementById('form1');
y = x.elements["name"].value;
z = x.elements["message"].value;

document.getElementById("demo1").innerHTML=y;
document.getElementById("demo2").innerHTML=z;
}
body {
background-color: lightgrey;
}
<form id = "form1">
Name: <input name = "name" type = "text" size = "20" id = "text1"/>
Message: <input name = "message" type = "text" size ="20" id = "text2"/>
</form>

<button onclick="outputdisplayname()">Display</button>

<p id = "demo1"></p>
<p id = "demo2"></p>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

最佳答案

改变

document.getElementById("demo1").innerHTML = y;

document.getElementById("demo1").innerHTML += y;

您只需在单击按钮之前确保 demo1 为空即可。

关于javascript - 来自同一个按钮的多次打印? html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56219689/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com