简体   繁体   English

如何验证既不转到另一页却又在同一页上填写另一张表格的表格?

[英]How do I validate a form that doesnt go to another page yet fills out another form on the same page?

I have two forms on a page, the second form should be filled out and have validation required to fill out most of the input fields. 我在页面上有两个表单,第二个表单应该填写,并且需要验证才能填写大部分输入字段。 If the fields arent filled out, then a pop-up or alert should be shown once the save button has been clicked, notifying the user of what needs to be done. 如果没有填写这些字段,则在单击保存按钮后应显示一个弹出窗口或警报,通知用户需要做什么。

Once the saved button has been clicked, the correctly filled out textfields should be copied/transferred/moved to the first form. 单击保存的按钮后,应将正确填写的文本字段复制/传输/移动到第一个表单。 For example; 例如; in the second form if I fill out title, name and surname. 在第二个表格中,如果我填写标题,姓名和姓氏。 Those three fields need to be validated. 这三个字段需要验证。 Once I hit the save-button, the values in the title, name and surname field should be concatenated inside of the name textfield in the first form. 按下保存按钮后,标题,名称和姓氏字段中的值应在第一种形式的名称文本字段内串联起来。

I've spotted to issues with your code: 我发现您的代码存在问题:

  • if you want to var a = document.forms["forms"]["Title"].value; 如果要var a = document.forms["forms"]["Title"].value; then "forms" value must be set on the id attribute instead of name attribute. 那么必须在id属性而不是name属性上设置“ forms”值。
  • document.getElementById('name').innerText = won't change the value of the name input, you have to use the value property instead. document.getElementById('name').innerText =不会更改名称输入的value而必须使用value属性。

Corrected code snippet below 更正了以下代码段

 function openForm() { document.getElementById("container").style.display = "block"; } document.getElementById("save-btn").onclick = function (e) { e.preventDefault(); validateForm(); moveContent(); } function moveContent() { var x = document.getElementById('name').value = document.getElementById('TitleTransfer').value + ' ' + document.getElementById('NameTransfer').value + ' ' + document.getElementById('LastNameTransfer').value; } function validateForm() { var a = document.forms["forms"]["Title"].value; var b = document.forms["forms"]["NameTransfer"].value; var c = document.forms["forms"]["LastNameTransfer"].value; var d = document.forms["forms"]["EmailTransfer"].value; if (a == null || a == "", b == null || b == "", c == null || c == "", d == null || d == "") { alert("Please Fill All Required Field"); return false; } } function openPage(pageName, elmnt, color) { var i, tabcontent, tablinks; tabcontent = document.getElementsByClassName("tabcontent"); for (i = 0; i < tabcontent.length; i++) { tabcontent[i].style.display = "none"; } tablinks = document.getElementsByClassName("tablink"); for (i = 0; i < tablinks.length; i++) { tablinks[i].style.backgroundColor = ""; } document.getElementById(pageName).style.display = "block"; elmnt.style.backgroundColor = color; } // Get the element with id="defaultOpen" and click on it document.getElementById("defaultOpen").click(); 
 <h1>Your Name and Address</h1> <form action="/"> <div class="nameDiv"> Name: <input type="textbox" name="firstname" title="name" id="name"> <div class="dotOne"> ? </div> <span class="nameHelp"> Please enter first and last name </span> </div> <br> <div class="addressDiv"> Address: <input type="text" name="lastname" id="address"> <div class="dotTwo">?</div> </div> <br> <div class="emailDiv"> Email: <input type="text" name="Email" id="email"> <span class="dotThree">?</span> </div> <br> <div class="numberDiv"> Phone number: <input type="text" name="Phone number" id="number"> <span class="dotFour">?</span> </div> <br> </form> <button class="btn" onclick="openForm()">Edit Details</button> <div id="container"> <button class="tablink" onclick="openPage('Name', this, 'grey')" id="defaultOpen">Name</button> <button class="tablink" onclick="openPage('Address', this, 'grey')">Address</button> <div id="Name" class="tabcontent"> <form id="forms"> <div class="nameDiv"> Title: <input type="text" name="Title" title="Title" id="TitleTransfer"> </div> <br> <div class="addressDiv"> Name: <input type="text" name="name" id="NameTransfer"> </div> <br> <div class="emailDiv"> Surname: <input type="text" name="surname" id="LastNameTransfer"> </div> <br> <div class="numberDiv"> Email Address: <input type="text" name="email Address" id="EmailTransfer"> </div> <br> <div class="numberDiv"> Phone number: <input type="text" name="Phone number" id="NumberTransfer"> </div> <button id="save-btn" onclick="validateForm()">Save</button> </form> </div> <div id="Address" class="tabcontent"> <form> <div class="nameDiv"> Line 1: <input type="text" name="firstname" value="Mickey" title="name" class="name"> </div> <br> <div class="addressDiv"> Line 2: <input type="text" name="lastname" value="Mouse" class="address"> </div> <br> <div class="emailDiv"> Suburb: <input type="text" name="Email" value="Email" class="email"> </div> <br> <div class="numberDiv"> State: <input type="text" name="Phone number" value="Phone number" class="number"> </div> <br> <div class="numberDiv"> Country: <input type="text" name="Phone number" value="Phone number" class="number"> </div> <button class="save-btn" onclick="">Save</button> </form> </div> </div> 

As asked by the author, this is a working fiddle implementation of the problem, with the css provided in another fiddle by the author, where the second form dissappear after having clicked "save". 正如作者所问的那样,这是该问题的有效提琴实现,作者在另一个提琴中提供了css,其中第二种形式在单击“保存”后消失了。

Working fiddle with the css 用CSS摆弄

暂无
暂无

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

相关问题 如何在重定向到另一个页面之前验证我的表单? - How do I validate my form before redirecting to another page? 如何选择表格到另一个页面? - How do I make a form selection go to another page? 我编写了用户填写的 HTML 表单,但是如果他们再次访问该页面,我如何证明他们已经提交了该表单? - I coded an HTML form that a user fills out, but how do I show that they already sumbitted it if they access that page again? 表格,使用php从mysql检索数据,并在同一HTML页面中填充另一表格 - Form which retrieves data from mysql using php and fills another form in the same HTML page 如果表单已验证,如何将 go 转到另一个 HTML 页面,如果未验证则留在同一页面? - How to go to another HTML page, if form is validated and stay in the same page if it is not validated? 如何在表单中设置动作属性以转到同一HTML页面中的另一表单,以及如何将第一种形式的值转换为第二种形式? - how to set action property in a form to go to another form in same html page and how to get values in first form into second form? 如何根据值验证表单并相应地将标注提交到另一个页面? - How to validate form based on values and submit call out to another page accordingly? 提交表单后如何将用户重定向到另一个页面? - How do I redirect user to another page after submit the form? 如何从另一个页面提交表单? - How do I submit a form from another page? 与其他表单在同一页面上的下拉表单导航 - Dropdown form navigation on the same page as another form
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM