【问题标题】:Require at least one checkbox with Parsley至少需要一个带有 Parsley 的复选框
【发布时间】:2018-05-29 15:52:05
【问题描述】:

我需要强制用户用欧芹检查至少一个不同名称的复选框。

使用相同的名称它可以工作:

<label for="hobbies">Hobbies:</label>
<p>
    Skiing <input name="hobbies[]" id="hobby1" value="ski" data-parsley-required type="checkbox"><br>
    Running <input name="hobbies[]" id="hobby2" value="run" type="checkbox"><br>
    Eating <input name="hobbies[]" id="hobby3" value="eat" type="checkbox"><br>
    Sleeping <input name="hobbies[]" id="hobby4" value="sleep" type="checkbox"><br>
    Reading <input name="hobbies[]" id="hobby5" value="read" type="checkbox"><br>
    Coding <input name="hobbies[]" id="hobby6" value="code" type="checkbox"><br>
</p>

但名称不同:

<label for="hobbies">Hobbies :</label>
<p>
    Skiing <input name="hobbies1" id="hobby1" value="ski" data-parsley-required type="checkbox"><br>
    Running <input name="hobbies2" id="hobby2" value="run" type="checkbox"><br>
    Eating <input name="hobbies3" id="hobby3" value="eat" type="checkbox"><br>
    Sleeping <input name="hobbies4" id="hobby4" value="sleep" type="checkbox"><br>
    Reading <input name="hobbies5" id="hobby5" value="read" type="checkbox"><br>
    Coding <input name="hobbies6" id="hobby6" value="code" type="checkbox"><br>
</p>

有没有办法用 data-parsley-group 或其他东西来管理这个?

注意:输入列表可以很长

【问题讨论】:

    标签: parsley.js


    【解决方案1】:

    我认为您可以通过赋予它们相同的data-parsley-multiple 值来使它们相关联。

    另一种可能性是编写自定义验证器something like this

    【讨论】:

      【解决方案2】:

      需要至少一个带有 Parsley 的复选框:

      data-parsley-required data-parsley-mincheck="1"

      Parsley docs on this

      <label for="hobbies">Hobbies (Required, 1 minimum):</label>
      <p>
        Skiing <input type="checkbox" name="hobbies[]" id="hobby1" value="ski" data-parsley-required data-parsley-mincheck="1"><br>
        Running <input type="checkbox" name="hobbies[]" id="hobby2" value="run"><br>
        Eating <input type="checkbox" name="hobbies[]" id="hobby3" value="eat"><br>
        Sleeping <input type="checkbox" name="hobbies[]" id="hobby4" value="sleep"><br>
        Reading <input type="checkbox" name="hobbies[]" id="hobby5" value="read"><br>
        Coding <input type="checkbox" name="hobbies[]" id="hobby6" value="code"><br>
      </p>
      

      【讨论】:

        猜你喜欢
        • 2015-08-12
        • 2015-06-27
        • 2015-01-14
        • 1970-01-01
        • 2018-04-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-22
        相关资源
        最近更新 更多