2013-06-29 84 views
3

我想用parsley.js验证单选按钮。以下是我正在尝试使用的代码使用parsley.js验证单选按钮

<p id="registration_field_custom2" class="onefield registration_field_custom2"> 
    <span class="doyou"> 
    <span class="text">etc etc etc etc ?</span> 
    <input type="radio" name="custom2" value="Yes" id="custom2_yes" 
     data-required="true" 
     data-error-container="#submitDetails .error6" 
     data-error-message ="Please chose either yes or no" 
    /> 
    <label for="custom2_yes">Yes</label> 
    <input type="radio" name="custom2" value="No" id="custom2_no" 
    /> 
    <label for="custom2_no">No</label>  
    </span> 
    </p> 

但是,无论我选择是或否,它仍在查找两者都需要的数据。我只是希望用户能够选择是或否......如何使用parsley.js来做到这一点?

回答

4

我认为对于无线电输入和复选框是特殊情况,而不是使用所需的数据,您应该使用data-mincheck来确保至少有一个选项被选中。

14

要验证欧芹中的单选按钮,您只需使用 - data-required =“true”。您不需要使用数据最小化检查单选按钮。由于单选按钮的名称相同,欧芹会自动识别它们在同一组中。 有很好的例子的欧芹文档中:http://parsleyjs.org/doc/index.html

实施例:

<input id="radio-required" class="parsley-validated" type="radio" data-required="true" value="foo" name="radiorequired"> 
<input id="radio-required2" type="radio" value="bar" name="radiorequired"> 
+0

这应选择答案。感谢代码示例 – geedubb

+0

这只适用于第一个单选按钮的父级的错误类... – Loenix

0

数据mincheck适用于复选框验证。

数据香菜要求的=“真正的”工程选项验证

http://parsleyjs.org/doc/examples/simple.html

+0

尽管此链接可能回答此问题,但仅在Stack Overflow上链接的答案是不鼓励的,您可以通过将重要部分链接并将其放入您的答案,这确保您的答案仍然是一个答案,如果链接被更改或删除:) – WhatsThePoint