![](/img/trans.png)
[英]How to validate that at least one checkbox is checked in each set of checkboxes
[英]How to validate if there is at least one checkbox selected out of 3 checkboxes in angularjs
好的,這是代碼:
<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>
您可以在輸入中添加“ 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}
您可以嘗試使用!frmLoanRefer.LoanReferData.Prop1代替frmLoanRefer.chkbx1。$ dirty。 frmLoanRefer.chkbx2。$ dirty和frmLoanRefer.chkbx3。$ dirty相同
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.