gpt4 book ai didi

javascript - 如何在AngularJS中进行验证?

转载 作者:行者123 更新时间:2023-11-28 17:42:50 24 4
gpt4 key购买 nike

我是 angularjs 验证的新手,并且正在尝试这样做。这是对的吗?谁能告诉我吗?

var app=angular.module('myApp',[])
app.controller('myController',function($scope){
$scope.clickMe = function(){
if($('#myForm .required').length==0){
alert("enter all details")
}

}
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
<form id="myForm" >
<input type="text" class="required">
<input type="date" class="required">
<select class="required">
<option>AAA</option>
<option>BBB</option>
</select>
</form>
<button ng-click="clickMe()">submit</button>
</div>

最佳答案

我已在您的代码中添加了必填字段的验证。要实现其他验证,请引用 Angular docs

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myController">
<form id="myForm" name="form1" ng-submit="form1.$valid && clickMe()">
<input type="text" class="required" name="text1" ng-model="model1" required>
<span ng-show="form1.text1.$error.required">Required</span>
<input type="date" class="required" name="text2" ng-model="model2" required>
<span ng-show="form1.text2.$error.required">Required</span>
<select class="required">
<option>AAA</option>
<option>BBB</option>
</select>
</form>
<button type="submit">submit</button>
</div>

关于javascript - 如何在AngularJS中进行验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47525196/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com