简体   繁体   English

JS删除class显示无不起作用

[英]JS remove class display none doesn't work

I have a strange problem with a "remove" class in JS.我对 JS 中的“删除”class 有一个奇怪的问题。 I have a div with the style "display: none" and when I click a button I want to remove that class, my JS works for one second, I mean, I see the div and after that, it disappears again.我有一个样式为“显示:无”的 div,当我单击一个按钮时,我想删除该 class,我的 JS 工作了一秒钟,我的意思是,我看到了 div,然后它又消失了。 I leave here the codepen link with the code.我在这里留下带有代码的 codepen 链接。

<div class="container">
    <div class="col-4 offset-4 text-center">
        <h2>My to-do list</h2>
    </div>
</div>

<div class="container" id="signin-form">
    <div class="col-6 offset-3 align-self-center mt-2">
        <h3 class="text-center">Sign in form</h3>
        <form>
            <div class="mb-3">
                <label for="inputEmail" class="form-label">Email address</label>
                <input type="email" class="form-control" id="inputEmail" aria-describedby="emailHelp">
            </div>
            <div class="mb-3">
                <label for="inputPwd" class="form-label">Password</label>
                <input type="password" class="form-control" id="inputPwd">
            </div>
            <div class="row mt-4" style="text-align: center;">
                <div class="col-6">
                    <button id="login" class="btn btn-outline-primary">Login</button>
                </div>
                <div class="col-6">
                    <button id="signUp" class="btn btn-outline-success">Sign up</button>
                </div>
            </div>
            <p class="mt-4 mb-4 small">If you already have an account please fill in the form and press "Login" otherwise press "Sign up"</p>
        </form>
    </div>
</div>

<div class="container noShow" id="register-form">
    <div class="col-6 offset-3 align-self-center mt-2">
        <h3 class="text-center ">Form for new user's registration</h3>
        <form>
            <div class="mb-3">
                <label for="name " class="form-label ">Name (*)</label>
                <input type="text " class="form-control " id="newName" required>
            </div>
            <div class="mb-3">
                <label for="surname " class="form-label ">Surname</label>
                <input type="text " class="form-control " id="newSurname">
            </div>
            <div class="mb-3">
                <label for="inputEmail " class="form-label ">Email address (*)</label>
                <input type="email " class="form-control " id="newEmail" aria-describedby="emailHelp " required>
            </div>
            <div class="mb-3">
                <label for="inputPwd " class="form-label ">Password (*)</label>
                <input type="text " class="form-control " id="newPwd" required>
            </div>
            <div class="row mt-4" style="text-align: center;">
                <div class="col-4 offset-4 ">
                    <button type="submit " id="save" class="btn btn-outline-success ">Save</button>
                </div>
            </div>
            <p class="mt-4 mb-4 small ">(*) Field are required</p>
        </form>
    </div>
</div>

https://codepen.io/dome68/pen/wWLzpE https://codepen.io/dome68/pen/wWLzpE

Thanks.谢谢。

Add e.preventDefault() to your event handler to prevent re-loading of the page post form submission like so:-e.preventDefault()添加到您的事件处理程序中,以防止重新加载页面发布form提交,如下所示:-

function signUpFn(e) {
    e.preventDefault();
    document.getElementById("register-form").classList.remove("noShow");
    document.getElementById("signin-form").classList.add("noShow");
}

The step to remove the class did work.移除 class 的步骤确实有效。 That's why you see it works for one second .这就是为什么你看到它工作了一秒钟

The problem is that the button submits the form later, that's why you see it disappears again .问题是按钮稍后提交表单,这就是您看到它再次消失的原因。

submit is the default value of type of a <button> associated with a <form> if the attribute is not specified.如果未指定属性,则submit是与<form>关联的<button> type的默认值。

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button

To fix that, assign a type="button" attribute to your Sign Up button which makes it a normal button that will not submit the form.要解决此问题,请为您的注册按钮分配一个type="button"属性,使其成为不会提交表单的普通按钮。

<button type="button" id="signUp" class="btn btn-outline-success">Sign up</button>

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

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