JavaScript - Bootstrap Validator

时间:2015-12-07 00:37:12

标签: javascript jquery

我正在使用此插件:Plugin Link

我正在尝试验证,如果已选中复选框组中的至少一个复选框。该插件不支持此类功能。因此我通过插件作者自己搜索并发现了这一点:Discussion Link以及此处的工作实现:Example

我尝试实现它并失败了。这就是我到目前为止所做的:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:sec="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd">


<bean id="httpSessionSecurityContextRepository"
    class='org.springframework.security.web.context.HttpSessionSecurityContextRepository'>
    <property name='allowSessionCreation' value='false' />
</bean>

<bean id="securityContextPersistenceFilter"
    class="org.springframework.security.web.context.SecurityContextPersistenceFilter">
    <constructor-arg ref="httpSessionSecurityContextRepository" />
</bean>

<bean id="filterChainProxy" class="org.springframework.security.web.FilterChainProxy">
    <constructor-arg>
        <list>
            <sec:filter-chain pattern="/rest/**"
                filters="securityContextPersistenceFilter" />
        </list>
    </constructor-arg>
</bean>

<sec:global-method-security
    authentication-manager-ref="authenticationManager"
    secured-annotations="enabled" />

另外这是复制的JS函数,应该做的魔术......:

{{1}}

所以,是的。如果我尝试运行它,没有任何反应。我的调试输出都没有打印在控制台中。没有。表单本身也包含一些密码字段和文本字段,复选框组 - 在foreach循环中生成 - 只是其中的一部分。验证器适用于文本和密码字段,但对复选框组没有任何作用。有什么想法吗?

谢谢! :)

1 个答案:

答案 0 :(得分:1)

我只是试着让它变得整洁 请查看解决方案:

参考:http://1000hz.github.io/bootstrap-validator/

&#13;
&#13;
$('#form').validator().on('submit', function (e) {
  var validate = false;
  $("input[type='checkbox']").each(function(index,e){
    
    if($(e).is(':checked'))
      validate = true;
  });
  
  if(validate){
    //valid
    $('.with-errors').html(' ');
  } else {
    $('.with-errors').html('not valid');

  }
  
  //if (e.isDefaultPrevented()) {
    // handle the invalid form...
  //} else {
    // everything looks good!
  //}
})
&#13;
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet"/>
<script src="http://1000hz.github.io/bootstrap-validator/dist/validator.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>





<form  role="form" data-toggle="validator" id="form" action="" method="POST">
<div class="col-lg-9">


  <div class="form-group">
    <div class="checkbox">
      <label>
        <input type="checkbox" name="complaints[]" data-chkgrp="complaints[]" data-error="Try selecting at least one...">
        Teste1
      </label>
    
      <div class="help-block  "></div>
    </div>
  </div>
  
  <div class="form-group">
    <div class="checkbox">
      <label>
        <input type="checkbox" name="complaints[]" data-chkgrp="complaints[]" data-error="Try selecting at least one...">
        Teste2
      </label>
    
      <div class="help-block with-errors"></div>
    </div>
  </div>
  
  <div class="form-group">
    <div class="checkbox">
      <label>
        <input type="checkbox" name="complaints[]" data-chkgrp="complaints[]" data-error="Try selecting at least one...">
        Teste3
      </label>
    
      <div class="help-block with-errors"></div>
    </div>
  </div>
  

</div>
  <button type="submit" >Validade</button>
  </form>
&#13;
&#13;
&#13;