通过JQuery设置输入字段值会使AngularJS上所需的验证失败
Setting input field value through JQuery makes required validation fail on AngularJS
我有一个标记为必需的输入字段,如果用户触摸了该字段但没有提供值,或者表单已提交,则会显示一条验证消息。它非常有效。
如果我用程序设置字段的值(例如使用JQuery),则仍会显示所需的消息。这是我的代码
<form
name="myForm"
novalidate>
<input
id="myField"
name="myField"
ng-model="myField"
type="text"
required />
<br />
<div
ng-messages="myForm.myField.$error"
ng-if="myForm.myField.$touched || myForm.$submitted">
<div ng-message="required">Required field</div>
</div>
<br />
<input
type="submit"
value="Send" />
</form>
<script>
$(function() {
$('#myField').val("this is a value");
});
var app = angular.module('app', [ 'ngMessages' ]);
app.controller('MyCtrl', function MainCtrl() {
});
</script>
为输入字段生成的html代码显示以下类:
<input id="myField" name="myField" ng-model="myField" type="text" required="" class="ng-pristine ng-invalid ng-invalid-required ng-touched">
我本可以为此创建一个浮游生物或小提琴手,但我无法将Angular Messages模块挂在上面。
这对我有效:
$scope.$apply(function(){
$scope.myForm.myField.$setValidity("required", true);
});
角度1.3.14
相关文章:
- 添加@javascript标记时,Cucumber中的HTTP身份验证失败
- postmates api的AJAX中的HTTP身份验证失败
- Spring Security中的基本身份验证(身份验证失败消息)
- 同一页面上的多个验证码在提交失败时消失
- 表单验证失败,但仍提交
- ExtJs 4 GridPanel with CellEdit:输入的数据在验证失败时丢失
- 验证失败时禁用按钮
- 即使验证失败,表单也会被提交
- jQuery验证另一个值失败
- 即使验证失败,表单也会提交
- jQuery长度验证失败
- 当表单验证失败时,我将如何最好地处理下一个事件
- 验证失败时阻止表单提交(Angular Material)
- OAuth错误:签名验证失败
- 如果在ASP.Net MVC视图中验证失败,请设置httppostedfilebase
- reCaptcha-验证失败
- 循环遍历所有字段,如果任何一个字段的验证失败jquery,则返回false
- 全局变量设置在JavaScript验证中失败
- 验证用户名和密码失败-客户端会话
- jQuery 在多步骤表单上验证失败后中断