2016-02-02 151 views
-1

我無法讓jQuery忽略某些字段。根據文件,所有應該都可以,但事實並非如此。JQuery不會忽略字段

默認和特定設置被設置爲與則會忽略.IGNORE類元素:

$(document).ready(function() { 

    $('form').validate({ 
     ignore: '.ignore', 
     rules: { 
      //tenant 
      'firstName': { required: true, minlength: 2 }, 
      'lastName': { required: true, minlength: 2}, 
      'email': { required: true, email: true }, 
      'phone': { required: true, digits: true}, 
      'password': { required: true, minlength: 5 }, 

      //apartment 
      'intercom': { required: true,digits: true, minlength: 4, maxlength: 4}, 
      'apartmentNumber': { required: true, digits: true}, 
      'description': { required: true }, 
      //meters 
      'description': { required: true}, 
      'serialNumber': { required: true}, 
      'unit': { required: true }, 
      'apartment': { required: true }, 
      //invoice 
      'serialNumber': { required: true}, 
      'totalAmount': { required: true, number: true} 

和DE默認設置:

$.extend($.validator, { 

    defaults: { 
     messages: {}, 
     groups: {}, 
     rules: {}, 
     errorClass: "error", 
     validClass: "valid", 
     errorElement: "label", 
     focusInvalid: true, 
     errorContainer: $([]), 
     errorLabelContainer: $([]), 
     onsubmit: true, 
     ignore: ".ignore, :hidden", 
     ignoreTitle: false, 
     onfocusin: function(element, event) { 
      this.lastActive = element; 

這裏是登錄頁面,原因很明顯,我想關閉密碼驗證。作爲絕望的行爲,我開始在每一個元素插入「忽略」類希望它最終工作:

<%@ page language="java" contentType="text/html; charset=UTF-8" 
    pageEncoding="UTF-8"%> 
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Login page</title> 
<link class="row" href="<c:url value='/static/css/bootstrap.css' />" 
    rel="stylesheet"> 

<link class="row" href="<c:url value='/static/css/style.css' />" 
    rel="stylesheet"> 

<script type="text/javascript" 
    src="<c:url value='/static/js/jquery-2.2.0.js' />"></script> 
<script type="text/javascript" 
    src="<c:url value='/static/js/bootstrap.js' />"></script> 
<script type="text/javascript" 
    src="<c:url value='/static/js/jquery.validate.js' />"></script> 
<script type="text/javascript" src="<c:url value='/static/js/jq.js' />"></script> 



<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<meta name="viewport" content="width=device-width, initial-scale=1.0"> 
</head> 

<body> 
    <div class="container"> 
     <div class='row'> 
      <div class='jumbotron'> 
       <h1>Kamienica</h1> 
       <h3>Podaj login i hasło</h3> 
      </div> 
     </div> 
     <div class='row'> 

      <div class="login-container"> 
       <div class="login-card ignore"> 
        <div class="form ignore"> 
         <c:url var="loginUrl" value="/login" /> 
         <form action="${loginUrl}" method="post" 
          class="form-horizontal form ignore"> 
          <c:if test="${param.error != null}"> 
           <div class="alert alert-danger"> 
            <p>Invalid username and password.</p> 
           </div> 
          </c:if> 
          <c:if test="${param.logout != null}"> 
           <div class="alert alert-success"> 
            <p>You have been logged out successfully.</p> 
           </div> 
          </c:if> 
          <div class="input-group input-sm"> 
           <label class="input-group-addon ingore" for="email"><i 
            class="glyphicon glyphicon-user ignore"></i></label> <input type="text" 
            class="ignore form-control" id="email" name="email" 
            placeholder="Enter Username" required> 
          </div> 
          <div class="input-group input-sm ignore"> 
           <label class="input-group-addon ingore" for="password"><i 
            class=" glyphicon glyphicon-lock ingore"></i></label> <input 
            type="password" class="form-control ignore" id="password" 
            name="password" placeholder="Enter Password" required> 
          </div> 
          <input type="hidden" name="${_csrf.parameterName}" 
           value="${_csrf.token}" /> 

          <div class="form-actions"> 
           <input type="submit" 
            class="btn btn-block btn-primary btn-default" value="Log in"> 
          </div> 
         </form> 
        </div> 
       </div> 
      </div> 
     </div> 
    </div> 

</body> 
</html> 

如果你能在正確的方向指向我,我將不勝感激。

問候,馬蒂厄

回答

0

我想你應該在input添加ignore類,但不label

[更新]
你使用的是jquery validation plugin?如果是的話,你應該只在html中或js中設置驗證規則。您在html元素中使用了像requiredmin這樣的規則,並且也使用了js規則。我不確定哪一個會被使用。但你應該只使用一種方法。

+0

它... 類=「形式的控制忽略」 忽略類加入到標貼後的去除需要在HTML中並沒有改變任何事情絕望 – Maciej

+0

的行爲... – Maciej

-1

對不起。我已經切換到最新的插件,所有問題都解決了。

感謝您的幫助。

中號