简体   繁体   English

如何显示和隐藏表单?

[英]How to show and hide a form?

When I load a page, I need to show form for name, but when I click "submit" i need hide that form.当我加载页面时,我需要显示名称表单,但是当我单击“提交”时,我需要隐藏该表单。 How can i do that with javascript?我怎么能用 javascript 做到这一点?

<div id="small-form">
    <form>
        <label for="fname">First name:</label><br>
        <input type="text" id="fname" name="fname"><br>
        <input type="submit" value="Submit" onclick="hideForm()">
    </form>
</div>

In the hideForm() function set display:none style for the div small-form .hideForm() function 中为 div small-form设置display:none样式。

Like that:像那样:

var myFormDiv = document.getElementById("small-form");
myFormDiv.style.display = "none";

You cannot just hide the form if it submits to the server unless you either Ajax the form to the server or target an iframe or new tab如果表单提交到服务器,则不能只隐藏表单,除非您将表单 Ajax 发送到服务器或定位到 iframe 或新选项卡

When the form submits, a new page is loaded.当表单提交时,会加载一个新页面。 If it loads the same page, you can either hide it on the server or set an instruction in localStorage to tell the page to not show the form如果它加载相同的页面,您可以将其隐藏在服务器上或在 localStorage 中设置指令告诉页面不显示表单

I also strongly suggest you do NOT use onclick of a submit button but the submit event我还强烈建议您不要使用提交按钮的 onclick 而是使用提交事件

 document.getElementById("myForm").addEventListener("submit",function(e) { e.preventDefault(); // if you want to NOT submit the form document.getElementById("small-form").classList.add("hide"); // here you can ajax or do other stuff without submitting })
 .hide { display: none; }
 <div id="small-form"> <form id="myForm"> <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname"><br> <input type="submit" value="Submit"> </form> </div>

If you wish to permanently hide the form you can do something like this.如果您希望永久隐藏表单,您可以执行以下操作。 Just set the display property of the form to none .只需将表单的display属性设置为none即可。 This will hide the form but it will exist there in the HTML code.这将隐藏表单,但它将存在于 HTML 代码中。

 function hideForm() { event.preventDefault(); var myForm = document.getElementById("My-Form"); myForm.style.display = "none"; var name = document.getElementById("Name"); alert(name.value); }
 <div id="small-form"> <form id="My-Form"> <label for="fname">First name:</label><br> <input id="Name" type="text" id="fname" name="fname"><br> <input type="submit" value="Submit" onclick="hideForm(event)"> </form> </div>

Another way to achieve what you are trying to do is simply remove the form.实现您想要做的另一种方法是简单地删除表单。 You can do this by calling remove() function on the form.您可以通过在表单上调用remove() function 来完成此操作。 This permanently removes the form.这将永久删除表单。

 function hideForm(event) { event.preventDefault(); var myForm = document.getElementById("My-Form"); var name = document.getElementById("Name"); alert(name.value); myForm.remove(); }
 <div id="small-form"> <form id="My-Form"> <label for="fname">First name:</label><br> <input id="Name" type="text" id="fname" name="fname"><br> <input type="submit" value="Submit" onclick="hideForm(event)"> </form> </div>

You can set the display style to none in the hideForm() function in javascript to hide the form.您可以在 javascript 中的hideForm() function 中将display样式设置为 none 以隐藏表单。

But, because you are using a submit button on the form it will try to redirect when the submit button is pressed.但是,因为您在表单上使用了提交按钮,所以它会在按下提交按钮时尝试重定向。 A simple solution to this (if you don't want the form to actually be submitted) is to change the type of the input to button rather than submit .一个简单的解决方案(如果您不希望实际提交表单)是将输入的类型更改为button而不是submit

 function hideForm() { document.getElementById('small-form').style.display = 'none'; }
 <div id="small-form"> <form > <label for="fname">First name:</label><br> <input type="text" id="fname" name="fname"><br> <input type="button" value="Submit" onclick="hideForm();"> </form> </div>

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

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