使用AngularJS验证单选按钮集

时间:2017-05-10 11:52:48

标签: javascript angularjs angular-validation

我的代码如下:

 <ng-form name="form">

    <div>
        Question 1
        <label>
            <input type="radio" ng-model="q1" style="visibility: initial; position: relative;"
                   ng-value="false">
               No
        </label>

        <label>
            <input type="radio" ng-model="q1" style="visibility: initial; position: relative;"
                   ng-value="true" ng-required="!(q1||q2||q3)">
                Yes
        </label>

    </div>

    <div  ng-show="!q1">
        Question 2 
        <br/>
        <label>
            <input type="radio" ng-model="q2" style="visibility: initial; position: relative;"
                   ng-value="false">
            No            </label>
        <br/>
        <label>
            <input type="radio" ng-model="q2" style="visibility: initial; position: relative;"
                   ng-value="true" ng-required="!(q1|| q2|| q3)">
                Yes            </label>
        <br/>
    </div>

    <div ng-show="!q1 && !q2">
       Question 3
        <br/>
        <label>
            <input type="radio" ng-model="q3" style="visibility: initial; position: relative;"
                   ng-value="false">
                No
        </label>
        <br/>
        <label>
            <input type="radio" ng-model="q3" style="visibility: initial; position: relative;"
                   ng-value="true" ng-required="!(q1|| q2|| q3)">
                Yes
        </label>
        <br/>

    </div>

    </ng-form>

所以,我需要一个(只有一个)“是”的答案来回答问题。但是,$ form.invalid始终为false。我看到'required =“required”'成功添加到输入字段时应添加,因此ng-required条件正常;)。

http://jsfiddle.net/ahL58/134/

1 个答案:

答案 0 :(得分:1)

按组给出无线电输入的名称。否则表格将无法检查验证