简体   繁体   English

通过单击链接更改div的活动状态

[英]change active state on div's by clicking a link

I'm currently having some issues with a Login-box. 我目前在登录框中遇到一些问题。 I'm using my own version with the logic behind a tutorial online . 我正在使用自己的版本以及在线教程背后的逻辑。 It all works fine right now. 现在一切正常。 When I click my Login button, a JQuery trigger displays the window which has the class active assigned to it. 当我单击“登录”按钮时,JQuery触发器将显示一个窗口,该窗口已为其分配了active类。 And once the login box has appears, the user could click on register in the login box and a register window becomes active. 一旦出现登录框,用户可以在登录框中单击注册,然后注册窗口变为活动状态。 I know i could be a bit hard to follow here but the question should be fairly easy. 我知道我可能在这里很难遵循,但问题应该很容易。 I want to press a new button next to LOGIN, that takes the user directly to REGISTER. 我想按一下LOGIN旁边的新按钮,使用户直接进入REGISTER。 For that I would need to set the active state to different forms depending on which button I press. 为此,我需要根据按下的按钮将active状态设置为不同的形式。 I'm trying to make a js script that senses which link has been click and then sets the active state to that form. 我正在尝试制作一个js脚本,该脚本可以检测到单击了哪个链接,然后将活动状态设置为该表单。 So the first Code is the links, right now both of them open the login box at the signin stage. 因此,第一个代码是链接,现在它们两个都在登录阶段打开登录框。 But my goal is to get the second one to open the register stage. 但是我的目标是让第二个人打开注册阶段。 Is this possible? 这可能吗? How can I set the active state to the right form in a click ? 单击如何将活动状态设置为正确的形式?

Here is an jsFiddle 这是一个jsFiddle

HTML 的HTML

<li><a href="#login-box" id="signin" class="linkform login-window">Login</a></li>
<li><a href="#login-box" rel="register" class="linkform login-window">Register</a></li>

<div id="login-box" class="login-popup"></div>
<form class="register" >    
    <input type="text" name="login" placeholder="email">
    <input type="password" name='password' placeholder="pass" required> 
    <input type="submit" name="submit" value="Reg">
    <a href="index.html" rel="signin" class="linkform forgot right">login</a>
</form>

<form class="signin active">
    <input type="text" name="login" placeholder="email">
    <input type="password" name='password' placeholder="pass" required>
    <input type="submit" name="submit" value="Login">
    <a href="register.html" rel="register" class="linkform forgot right">Reg</a>
</form>

Javascript Java脚本

$(function() {
    //the form wrapper (includes all forms)
    var $form_wrapper = $('#login-box'),
        //the current form is the one with class active
        $currentForm = $form_wrapper.children('form.active'),
        //the change form links
        $linkform = $form_wrapper.find('.linkform');

    $linkform.bind('click', function(e) {
        var $link = $(this);
        var target = $link.attr('rel');
        $currentForm.fadeOut(400, function() {
            //remove class active from current form
            $currentForm.removeClass('active');
            //new current form
            $currentForm = $form_wrapper.children('form.' + target);
            //animate the wrapper
            $form_wrapper.stop()
                .animate({
                        height: $currentForm.data('height') + 'px'
                    }, 500, function() {
                        //new form gets class active
                        $currentForm.addClass('active');
                        //show the new form
                        $currentForm.fadeIn(400);
                    });
        });
        e.preventDefault();
    });
});

I re-read your question, I think I hear where you are having problems: 我重新阅读了您的问题,我想我听到您遇到问题了:

// NOTE: add id="signinForm" and id="registerForm" to your forms, I suggest this over class="signin" and class="register" as you'll only have one signin/register form throughout your app.
// rename rel="register" to id="register", why are you using rel? http://www.w3schools.com/TAGS/att_a_rel.asp
$("#signin").click(function() {
    $("#registerForm").removeClass("active");
    $("#signinForm").addClass("active");
});
$("#register").click(function() {
    $("#signinForm").removeClass("active");
    $("#registerForm").addClass("active");
});

To access and submit your form, create another click event to your submit buttons: 要访问和提交表单,请在您的提交按钮上创建另一个click事件:

 $("input[type=submit]").click(function() {
    var form = $("form.active")[0];
    form.submit();
 });

why don't you use tabs? 为什么不使用标签?

<div id="tabs">
  <ul>
    <li><a href="#tabs-1">Login</a></li>
    <li><a href="#tabs-2">Register</a></li>
  </ul>
  <div id="tabs-1">
    <form class="login" >  
        <input type="text" name="login" placeholder="email">
        <input type="password" name='password' placeholder="pass" required> 
        <input type="submit" name="submit" value="Reg">
        <a href="index.html" rel="signin" class="linkform forgot right">login</a>
     </form>
  </div>
  <div id="tabs-2">
    <form class="register">  
        <input type="text" name="login" placeholder="email">
        <input type="password" name='password' placeholder="pass" required>         
        <input type="submit" name="submit" value="Login">
        <a href="register.html" rel="register" class="linkform forgot right">Reg</a>
    </form>
  </div>
</div>

JS: JS:

$(function() {
    $( "#tabs" ).tabs();
});

it should be pretty much easier and you get the desired behaviour. 它应该容易得多,您将获得所需的行为。

Greetings, 问候,

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

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