来自网站的欧芹示例不起作用?

时间:2015-04-13 15:05:05

标签: jquery html validation parsley.js customvalidator

我使用Parsley中的the documetation page site示例:

我在硬盘上保存了一个页面,其中包含以下内容:

<form id="demo-form" data-parsley-validate>
  <label for="question">Do you code? *</label>
  <p>
    <input type="radio" name="question1" value="true" required /> Yes
    <input type="radio" name="question1" value="false" /> No
  </p>

  <label for="languages">If yes, in which language(s)?</label>
  <input type="text" class="form-control" name="languages" data-parsley-conditionalrequired='["[name=\"question1\"]:checked", "yes"]' data-parsley-validate-if-empty data-parsley-success-class="" data-parsley-conditionalrequired-message="This value is required since you are a programmer!" />

  <label for="question">Do you eat dog food? *</label>
  <p>
    <input type="radio" name="question2" value="yes" required /> Yes
    <input type="radio" name="question2" value="no" /> No
  </p>

  <label for="why">If no, why?</label>
  <input type="text" class="form-control" name="why" data-parsley-conditionalrequired='["[name=\"question2\"]:checked", "no"]' data-parsley-validate-if-empty data-parsley-success-class="" data-parsley-conditionalrequired-message="This value is required since you do not eat dog food!" />

  <input type="submit" class="btn btn-default pull-right" />
</form>

<script type="text/javascript">
  window.ParsleyConfig = {
    validators: {
      conditionalrequired: {
        fn: function (value, requirements) {
          // if requirements[0] value does not meet requirements[1] expectation, field is required
          if (requirements[1] == $(requirements[0]).val() && '' == value)
          return false;

          return true;
        },
        priority: 32
      }
    }
  };
</script>
<script type="text/javascript" src="http://parsleyjs.org/dist/parsley.js"></script>

但是任何自定义的vaidator似乎都不起作用。

我的意思是,自定义验证器的逻辑不受尊重。 &#34;如果是,使用哪种语言?&#34;我选择是,并且不会填写语言......

问题出在哪里?

还有一个小提琴 HERE

1 个答案:

答案 0 :(得分:3)

更改

'["[name=\"question1\"]:checked", "yes"]'

<input type="text" class="form-control" name="languages" data-parsley-conditionalrequired='["[name=\"question1\"]:checked", "yes"]' data-parsley-validate-if-empty data-parsley-success-class="" data-parsley-conditionalrequired-message="This value is required since you are a programmer!" />

'["[name=\"question1\"]:checked", "true"]'

你必须使用value而不是

中的文字
<input type="radio" name="question1" value="true" required /> Yes

修改

根据评论,另一种(更一致的)解决方案是更改HTML输入value

<input type="radio" name="question1" value="yes" required /> Yes
<input type="radio" name="question1" value="no" /> No