[英]Angular.js / Using ng-submit with form
I have an HTML page with form block 我有一个带有表单块的HTML页面
<div>
<form>
<div class="row">
...
<input required> a </input>
...
<div>
<div class="row">
...
<input required> b </input>
...
<div>
<div>
<button ng-click="expand()"> Expand </button>
<button type="submit" ng-submit="create()"> Start </button>
</div>
</form>
</div>
When I click on start, and a or b are empties, message is shown: "Please fill out this field"
(which it is the desire behavior) . 当我单击开始时,并且a或b为空时,显示消息:
"Please fill out this field"
(这是期望的行为)。 But when I fill out this field, and click again, create()
is not submitted and I get the following error: An invalid form control with name='' is not focusable
. 但是,当我填写此字段并再次单击时,未提交
create()
,并且出现以下错误: An invalid form control with name='' is not focusable
。
I try also in this way: 我也这样尝试:
<div>
<form ng-submit="create()">
<div class="row">
...
</div>
<div>
<button ng-click="expand()"> Expand </button>
<button type="submit"> Start </button>
</div>
</form>
</div>
But then even if I click on Expand, create()
is executed instead of expand()
. 但是,即使我单击Expand,也会执行
create()
而不是expand()
。
I also try yo change ng-submit to ng-click, but then it doesn't validate that the fields is fill out. 我也尝试将ng-submit更改为ng-click,但是它不能验证字段是否已填写。
My purpose is that create()
will NOT be executed if one of the fields is empty, and proper message will be shown. 我的目的是如果其中一个字段为空,则不会执行
create()
,并且将显示正确的消息。
Try this: 尝试这个:
<div>
<form ng-submit="submit()">
<div class="row">
<input required type="text" ng-model="text" name="text_a">A</input>
</div>
<div class="row">
<input required type="text" ng-model="text" name="text_b">B</input>
</div>
<div>
<button ng-click="expand()"> Expand </button>
<input type="submit" id="submit" value="Create" />
</div>
</form>
</div>
The simple way to do form validation is like this: Give your form a name attribute 进行表单验证的简单方法是这样的:给表单命名属性
<form name="myForm">
Add the required attribute to your inputs. 将必需的属性添加到您的输入中。
Disable your button with 通过禁用按钮
<button ng-disable="myForm.$invalid" ng-click="create()">Submit</button>
If one of your inputs is invalid you can likewise display a message with a span or div that has the ng-show="myForm.$invalid" attribute. 如果您输入的内容之一无效,则可以同样显示具有ng-show =“ myForm。$ invalid”属性的span或div消息。
I'm not clear on exactly what you are doing with the data from the inputs. 我不清楚您对输入数据的确切处理方式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.