gpt4 book ai didi

javascript - 如果输入未验证,AngularJS 会阻止提交

转载 作者:可可西里 更新时间:2023-11-01 13:26:43 29 4
gpt4 key购买 nike

如果输入尚未验证,我试图阻止提交表单..

这是我的代码:

html

<form method="post" enctype="multipart/form-data" ng-submit="insertCustomer(insertProfile)" id="form-customer" name="form" class="css-form" novalidate >
<div class="row">
<div class="col-md-7">
<label>Full Name*
<span class="required-label" ng-show="form.$submitted || form.full_name.$touched">
<span ng-show="form.full_name.$error.required">(Full Name is required.)</span>
</span>
</label>
<input type="text" ng-model="insertProfile.full_name" name="full_name" class="form-control" required="" />
</div>
</div>
</form>

<button type="submit" form="form-customer"
class="btn btn-primary pull-right" id="cmd_insert_customer" ng-show="tab == 3">
Insert Customer
</button>

我试过使用 ng-submit = "form.$valid && insertCustomer(insertProfile)"...如果输入为空,表单不会提交,但是当我输入值时... .它仍然没有提交...

最佳答案

将表单传递给 insertCustomer 函数并在函数中检查有效性,就像这样

<form method="post" enctype="multipart/form-data" ng-submit="insertCustomer(form)" id="form-customer" name="form" class="css-form" novalidate >
<div class="row">
<div class="col-md-7">
<label>Full Name*
<span class="required-label" ng-show="form.$submitted || form.full_name.$touched">
<span ng-show="form.full_name.$error.required">(Full Name is required.)</span>
</span>
</label>
<input type="text" ng-model="insertProfile.full_name" name="full_name" class="form-control" required="" />
</div>
</div>
</form>

<button type="submit" form="form-customer"
class="btn btn-primary pull-right" id="cmd_insert_customer" ng-show="tab == 3">
Insert Customer
</button>

在js中这样检查

$scope.insertCustomer = function(form){
if(form.$invalid){
return;
}
//form submit code....
}

希望它有用。

关于javascript - 如果输入未验证,AngularJS 会阻止提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36928065/

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