[英]Show/Hide inline Form
I have an inline form on my webpage that is hidden by css when the page is loaded. 我的网页上有一个内联表单,该页面在加载页面时被CSS隐藏。 I also have a button to display the form.
我也有一个按钮来显示表单。 However, I want that same button to hide the form if it's clicked again.
但是,如果再次单击该表单,我希望该按钮隐藏该表单。 But I can't seem to get it working, instead when I click the inline form button the form disappears even though I never assigned a function to it.
但是我似乎无法正常工作,而是当我单击内联表单按钮时,即使我从未为其分配功能,该表单也会消失。
Here's my HTML 这是我的HTML
<button id="signup" onclick="showForm('inline')">SignUp!</button>
<!-- hidden inline form -->
<div id="inline">
<h2>Register! It's free!</h2>
<form id="register" action="#" method="post" name="register">
<label for="email">Email:</label>
<input id="email" class="txt" type="email" name="email" />
<label for="password">Password:</label>
<input id="password" class="passtxt" type="password" name="password" />
<label for="password">Repeat Password:</label>
<input id="password" class="passtxt" type="password" name="password" />
<button id="registerbtn">Register</button>
</form>
</div>
Here's my JS: 这是我的JS:
function showForm(form) {
var e = document.getElementById(form);
if (e.style.display = 'none')
e.style.display = 'inline';
else
e.style.display = 'none';
}
When I click the signup button, the form shows. 当我单击注册按钮时,将显示该表单。 However if I click it again nothing happens.
但是,如果我再次单击它,则什么也不会发生。 But if i click the register button in the form, it disappears.
但是,如果我单击表单中的注册按钮,它就会消失。 Why?
为什么? and how can I fix it?
以及如何解决?
EDIT: I chose DaveHogan's as the best answer as it was what I was looking for. 编辑:我选择了DaveHogan作为最佳答案,因为这就是我想要的。 But I have switched over to using JQuery instead, and if you're reading this.
但是,如果您正在阅读本文,那么我已经改用JQuery了。 You should switch too.
您也应该切换。
看来您没有正确检查相等性(使用双等于)。
if (e.style.display == 'none')
If you would use jQuery, you could use toggle :) 如果要使用jQuery,则可以使用toggle :)
http://jsfiddle.net/Se5bm/ http://jsfiddle.net/Se5bm/
<div id="foo">omg </div>
<button id="bar">toggle me</button>
$('#bar').bind('click' , function() {
$('#foo').toggle();
});
您必须记住,“ =”与“ ==”不同,=是要分配的,而==是要问的问题,诸如此类... A = B,在这种情况下,您将A等于B,如果您写A == B,则您在询问A是否等于B
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.