简体   繁体   English

如何在不提交表单的情况下在同一页面中显示表单数据?

[英]How I can display the form data in the same page without submit the form?

I have a form in HTML and I want to display the form text input data on the same page but before pressing the submit button.我在 HTML 中有一个表单,我想在同一页面上显示表单文本输入数据,但在按下提交按钮之前。 Mean, When Users put the data in the form it must display below the form on same page.意思是,当用户将数据放入表单时,它必须显示在同一页面的表单下方。 It's mean that I want to show all data before submitting the form.这意味着我想在提交表单之前显示所有数据。

I know this code will not work as i want我知道这段代码不会像我想要的那样工作

var strText = document.getElementById("textone");
document.write(strText.value);
var strText1 = document.getElementById("textTWO");
document.write(strText1.value);
}

This is how I would do it by directly manipulating the DOM:这就是我通过直接操作 DOM 的方式:

 const input = document.getElementById('textInput'); const textElement = document.getElementById('displayText'); function updateValue(e) { textElement.textContent = e.target.value; } input.addEventListener('input', updateValue);
 <input type="text" id="textInput"> <p>value from input:</p> <div id="displayText"></div>

There are also javascript libraries like VueJS and ReactJS that can help you do this more easily and efficiently.还有像VueJSReactJS之类的 javascript 库可以帮助您更轻松有效地完成此操作。

This is an example of something like what you would want to do in VueJS: https://v2.vuejs.org/v2/examples/index.html这是您想要在 VueJS 中执行的操作的示例: https://v2.vuejs.org/v2/examples/index.html

I've prepared an example of general functioning, I hope you like it.我准备了一个一般功能的例子,希望你喜欢。 It may not be exactly what you want, but if it is, please tell me.它可能不是你想要的,但如果是,请告诉我。

 const myForm = document.getElementById("myForm"); const nameInput = document.getElementById("nameInput"); const emailInput = document.getElementById("emailInput"); const nameOutput = document.getElementById("nameOutput"); const emailOutput = document.getElementById("emailOutput"); let nameSpan = document.getElementById("name"); let emailSpan = document.getElementById("email"); myForm.addEventListener("submit", e => { e.preventDefault(); alert(`NAME: ${nameInput.value}, EMAİL: ${emailInput.value}`) // select name, mail nameSpan.innerText = nameInput.value; emailSpan.innerText = emailInput.value; // clear ınputs nameInput.value = ""; emailInput.value = "" }) showData(); function showData() { nameInput.addEventListener("keyup", e => { nameOutput.value = e.target.value; }) emailInput.addEventListener("keyup", e => { emailOutput.value = e.target.value; }) }
 <form id="myForm"> <input type="text" id="nameInput" placeholder="your name"> <input type="text" id="emailInput" placeholder="your email"> <button type="submit" id="getInputValue"> Get Input Value </button> </form> <div id="values" style="margin-top: 100px;"> <input type="text" placeholder="NAME" id="nameOutput"> <input type="text" placeholder="EMAİL" id="emailOutput"> </div> <div> <p>Your name: <span id="name"></span></p> <p>Your email: <span id="email"></span></p> </div>

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

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