简体   繁体   English

检测HTML表单提交

[英]Detecting HTML form submit

I've got a generic form below that has a button and that button will post to a database, what the button does isn't really what this question is about, the question is about detecting an html form submit in angular. 我在下面有一个带有按钮的通用表单,该按钮将发布到数据库中,该按钮的作用不是这个问题的实质,问题是关于检测以角度提交的html表单。

<div class="example" ng-controller="ExampleController as app">
   <h1>Button below Form appears when fields full</h1>
   <form method="post" action="do_button_stuff"><TABLE BORDER="1">
     <TR><TD>Your name</TD>
     <TD><INPUT TYPE="TEXT" NAME="name" SIZE="20"></TD></TR>
     <TR><TD>Your E-mail address</TD>
     <TD><INPUT TYPE="TEXT" NAME="email" SIZE="25"></TD></TR>
   </TABLE></form>
   <input type="submit" value="Submit" ng-show="filled_form">
</div>

I'm a native PHP programmer and new to Angular and to Javascript, so I'm working on being a full stack dev. 我是本地PHP程序员,并且是Angular和Javascript的新手,所以我正在努力成为一个完整的堆栈开发人员。 I have used jQuery before. 我以前使用过jQuery。 But I'm out of my element and don't have a normal process flow as to how to get through this hurdle. 但是我没有自己的能力,也没有关于如何克服这一障碍的正常流程。 I suspect the solution is either Javascript or jQuery or adding the right unknown angular html-tags to the form DOM element . 怀疑解决方案是Javascript还是jQuery,或者在DOM元素表单中添加了正确的未知角度html标签 The idea is to provide a sort of client side validation, so that a user couldn't be able to use the post button unless they at least attempted to fill out the fields. 这个想法是要提供一种客户端验证,以便用户除非至少尝试填写字段,否则无法使用发布按钮。 Of course I have server side validations as well. 当然,我也有服务器端验证。

Is it necessary to find an answer for this with Angular.js, or should I keep it simple and use only javascript? 是否有必要使用Angular.js找到答案,还是应该保持简单并仅使用javascript?

You could add to your <form> ng-submit="here_function_name(variables)" and remember to move your button inside <form></form> tags. 您可以将其添加到<form> ng-submit="here_function_name(variables)"并记住将按钮移至<form></form>标记内。

After click on button, angular will execute here_function_name(variables) which should be defined in $scope in your controller. 单击按钮后,angular将执行here_function_name(variables) ,应在控制器的$scope中定义。 You can put your validation in this function. 您可以将验证放入此功能。

To get access to values in your inputs, there is example below (using ng-model ). 要访问输入中的值,请参见下面的示例(使用ng-model )。

See example of ng-submit and forms in angular here: https://docs.angularjs.org/api/ng/directive/ngSubmit 在此处查看ng-submit和angular形式的示例: https : //docs.angularjs.org/api/ng/directive/ngSubmit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM