简体   繁体   English

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

[英]How to validate if there is at least one checkbox selected out of 3 checkboxes in angularjs

Ok so this is the code: 好的,这是代码:

<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>

You can add an 'ng-true-value' attribute in your inputs as example 您可以在输入中添加“ 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}}

And here is an answer 这是一个答案

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

you can try with !frmLoanRefer.LoanReferData.Prop1 instead of frmLoanRefer.chkbx1.$dirty. 您可以尝试使用!frmLoanRefer.LoanReferData.Prop1代替frmLoanRefer.chkbx1。$ dirty。 The same for frmLoanRefer.chkbx2.$dirty and frmLoanRefer.chkbx3.$dirty frmLoanRefer.chkbx2。$ dirty和frmLoanRefer.chkbx3。$ dirty相同

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM