如何验证angularjs中的3个复选框中是否至少选中了一个复选框

时间:2016-01-21 01:37:28

标签: javascript jquery angularjs asp.net-mvc checkbox

好的,这就是代码:

<input name="chkbx1" type="checkbox"
    ng-model="LoanReferData.Prop1"
    ng-class="Submitted?'ng-dirty':''" required>Prop 1</input>

<input name="chkbx2" type="checkbox"
    ng-model="LoanReferData.Prop2" ng-class="Submitted?'ng-dirty':''"
    required>Prop 2</input>

<input name="chkbx3" type="checkbox" ng-model="LoanReferData.Prop3"
    ng-class="Submitted?'ng-dirty':''" required>Other</input>


<span class="error" ng-show="((frmLoanRefer.chkbx1.$dirty || Submitted) && frmLoanRefer.chkbx1.$error.required) || 
                                                            ((frmLoanRefer.chkbx2.$dirty || Submitted) && frmLoanRefer.chkbx2.$error.required) || 
                                                            ((frmLoanRefer.chkbx3.$dirty || Submitted) && frmLoanRefer.chkbx3.$error.required)  ">
                            * please select atleast 1 property is required.</span>

2 个答案:

答案 0 :(得分:1)

您可以在输入中添加“ng-true-value”属性作为示例

<input name="chkbx1" type="checkbox" ng-true-value="true"
                            ng-model="LoanReferData.Prop1"
                            ng-class="Submitted?'ng-dirty':''" required>Prop 1</input>

<input name="chkbx2" type="checkbox" ng-true-value="true"
                            ng-model="LoanReferData.Prop2" ng-class="Submitted?'ng-dirty':''"
                            required>Prop 2</input>

<input name="chkbx3" type="checkbox" ng-model="LoanReferData.Prop3" ng-true-value="true"
                            ng-class="Submitted?'ng-dirty':''" required>Other</input>

{{LoanReferData}}

这是一个答案

{"Prop1":"true","Prop2":"true","Prop3":false}

答案 1 :(得分:0)

你可以试试!frmLoanRefer.LoanReferData.Prop1而不是frmLoanRefer.chkbx1。$ dirty。 frmLoanRefer.chkbx2也是一样。$ dirty和frmLoanRefer.chkbx3。$ dirty