![](/img/trans.png)
[英]How do you make the fields read-only in a Django form when a specific condition is met?
[英]How do i display only the specific console when my condition inside loop is met
這是我的控制器功能:
$scope.Save = function () {
for(var i = 0; i < $scope.listOfnames.length; i++){
if($scope.listOfnames[i].surname == $scope.name.surname){
console.log("This surname is already exist!");
}else{
console.log("save this surname.");
}
}
}
這是我在控制台中的輸出:
save this surname.
This surname is already exist!
save this surname.
例如,我的 $scope.listOfnames 中已經有一個“STUART”姓氏,當我在我的姓氏輸入字段中輸入“STUART”姓氏時,它必須顯示“這個姓氏已經存在!” 僅在我的控制台中。
我知道我的 for 循環有問題,我應該怎么做?
您在控制台上得到它,因為“STUART”是陣列上的第二個值。 您可以執行以下操作:
$scope.Save = function () {
var found = false;
//Loop thru the array and check each. If found change the value of var found
for(var i = 0; i < $scope.listOfnames.length; i++){
if( $scope.listOfnames[i].surname == $scope.name.surname ){
found = true;
}
}
if ( found ) {
console.log("This surname is already exist!");
} else {
console.log("save this surname.");
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.