2016-06-14 43 views
0

我在同一頁面中有兩種形式,當點擊圖標時出現一個,登錄消失並顯示註冊。我使用彈簧MVC,所以我想在輸入/註冊切換窗體工作的功能。當進入其他web服務時切換JavaScript功能URL

<!DOCTYPE html> 
<html xmlns:th="http://www.thymeleaf.org"> 
<head> 
    <meta charset="UTF-8"/> 
    <title>Login Form</title> 

    <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" 
      href="../static/css/reset.css"/> 

    <link rel='stylesheet prefetch' 
      href='http://fonts.googleapis.com/css?family=Roboto:400,100,300,500,700,900|RobotoDraft:400,100,300,500,700,900'/> 
    <link rel='stylesheet prefetch' href='http://maxcdn.bootstrapcdn.com/font-awesome/4.3.0/css/font-awesome.min.css'/> 

    <!--<link rel="stylesheet" href="../static/css/style.css"/>--> 
    <link rel="stylesheet" th:href="@{/css/style.css}" 
      href="../static/css/style.css"/> 


</head> 

<body> 


<!-- Mixins--> 
<!-- Pen Title--> 

<div class="container"> 
    <div class="card"></div> 
    <div class="card"> 
     <h1 class="title">Login</h1> 
     <form action="login" th:action="@{/login}" th:object="${login}" method="post" > 
      <div class="input-container"> 
       <input type="email" id="email" th:field="*{email}" required="required"/> 
       <label for="email">email</label> 
       <div class="bar"></div> 
      </div> 
      <div class="input-container"> 
       <input type="password" id="Password" th:field="*{password}" required="required"/> 
       <label for="Password">Password</label> 
       <div class="bar"></div> 
      </div> 
      <div class="button-container"> 
       <button type="submit" name="action" value="login"><span>Go</span></button> 
      </div> 
      <div class="footer"><a href="#">Forgot your password?</a></div> 
     </form> 
    </div> 
    <div class="card alt"> 
     <div class="toggle"></div> 
     <h1 class="title">Register 
      <div class="close"></div> 
     </h1> 
     <form action="register" th:action="@{/register}" th:object="${register}" method="post"> 
      <div class="input-container"> 
       <input type="text" id="firstName" th:field="*{firstName}" required="required"/> 
       <label for="firstName">First Name</label> 
       <div class="bar"></div> 
      </div> 

      <div class="input-container"> 
       <input type="text" id="lastName" th:field="*{lastName}" required="required"/> 
       <label for="lastName">Last Name</label> 
       <div class="bar"></div> 
      </div> 

      <div class="input-container"> 
       <input type="email" id="email_" th:field="*{email}" required="required"/> 
       <label for="email">Email</label> 
       <div class="bar"></div> 
      </div> 
      <div class="input-container"> 
       <input type="password" id="Password_" th:field="*{password}" required="required"/> 
       <label for="Password">Password</label> 
       <div class="bar"></div> 
      </div> 
      <div class="button-container"> 
       <button type="submit" name="action" value="register"><span>Next</span></button> 
      </div> 
     </form> 
    </div> 
</div> 
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js'></script> 

<script th:src="@{/js/index.js}"></script> 


</body> 
</html> 

JavaScript代碼:

$('.toggle').on('click', function() { 
    $('.container').stop().addClass('active'); 
}); 

$('.close').on('click', function() { 
    $('.container').stop().removeClass('active'); 
}); 

我想例如進入/註冊它檢索登記表,而不是登陸

+0

GitHub上的示例項目將幫助我們理解問題。 –

+0

好吧,我已經添加了問題中的代碼:) –

+1

我看到兩種使用不同動作端點的表單。登錄表單使用「/ login」端點。註冊表格使用「/註冊」端點。這應該工作得很好。你有什麼具體問題? GitHub上的完整示例就是需要的。 –

回答

0

一種方法是創建一個/註冊控制器將返回一個參數:

@RequestMapping("/register") 
public ModelAndView register() { 
    ModelAndView mv = new ModelAndView("your_form_page") 
    mv.addObject("isRegister", true); 
    return mv; 
} 

然後你就可以添加一個隱藏字段,並用它在JavaScript切換的div:

<!DOCTYPE html> 
<html 
    xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" 
    xmlns:th="http://www.thymeleaf.org"> 

    <head> 
     <!-- imports omitted --> 
     <script type="text/javascript"> 
      $(document).ready(function() { 
       if ($("#isRegister").val()) { 
        $("#register").show(); 
        $("#signUp").hide(); 
       } else { 
        $("#register").hide(); 
        $("#signUp").show(); 
       } 
      }); 
     </script> 


    </head> 

    <body> 
     <input id="isRegister" type="hidden" th:value="${isRegister}" disabled="disabled"/> 


     <div id="signUp"> 
      Sign Up Form 
     </div> 
     <div id="register"> 
      Register Form 
     </div> 

    </body> 
</html> 

當然你也可以的JavaScript代碼片段移動到你的「的.js」文件。這裏重要的是有隱藏的輸入字段,可以在Javascript中訪問切換DIV。

+0

感謝它工作:) –