我有两个输入(复选框)。一个是给学生的,另一个是给顾问的。我想要至少需要一个或其他输入复选框。如果单击其中一个,我就会让它们切换。我只是不知道如何让它与表单验证的其余部分一起运行。
学生:
<input type="checkbox" class="userTypeClass" checked="checked" id="studentCheckbox" name="studentCheckbox" ng-true-value="1" ng-model="user.userType" ng-change="inputCheck(user)"/>
顾问:
<input type="checkbox" class="userTypeClass" id="consultantCheckBox" name="consultantCheckBox" ng-true-value="2" ng-model="user.userType" ng-change="inputCheck(user)"/>
注册按钮:
<button class="col button-green" ng-disabled="signupForm.$invalid && !flag" ng-click="submit(user)">Continue</button>
JS Controller 功能:
$scope.inputCheck = function(user) {
var flag = false;
if (user.userType == false) {
flag = true;
} else {
flag = false;
}
console.log(flag);
};
最佳答案
可能有更好的答案,但这里有一个想法-
<input type="checkbox" class="userTypeClass" checked="checked" id="studentCheckbox" name="studentCheckbox" ng-true-value="1" ng-model="user.userType" ng-change="inputCheck(user)"/>
<input type="checkbox" class="userTypeClass" id="consultantCheckBox" name="consultantCheckBox" ng-true-value="2" ng-model="user.userType" ng-change="inputCheck(user)"/>
<input type="hidden" required ng-model="user.userType"/>
通过隐藏输入,您可以确保至少有一个被检查。我不知道你的 inputCheck 方法的目的,但这是一种可以清理它的方法-
$scope.inputCheck = function(user) {
var flag = user.userType === false;
console.log(flag);
};
// or if you dont need to store the flag:
$scope.inputCheck = function(user) {
console.log(user.userType === false);
};
编辑:
嗯,我不知道 false 不起作用。你可以尝试一个隐藏的复选框 - (未经测试,不确定这是否有效)
<input type="checkbox" ng-hide="true" required ng-model="user.userType"/>
或者您可以取消设置 inputCheck
中的值,但这有点笨拙。
$scope.inputCheck = function(user) {
if(user.userType === false) {
user.userType = undefined;
}
};
关于javascript - 使用 Angular require 输入来启用注册 : require input's/validation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35108385/