简体   繁体   English

用于提交和验证表单的按钮,如果有效,则运行功能

[英]Button to submit and validate form, if valid then run a function

I have a form that currently when I click a button it validates the form. 我有一个表单,当前当我单击按钮时它会验证该表单。 But if the form validates I would like it to then toggle my accordion. 但是,如果表单通过验证,我希望它能切换我的手风琴。

So to validate my form I have: 因此,为了验证我的表格,我有:

<a href="#" ng-click="submitted=true" class="btn mainbutton">

Click me 点击我

Then to open my Accordion the code is: 然后打开我的手风琴的代码是:

ng-click="ctrl.toggle()" ng-click =“ ctrl.toggle()”

Just not sure how I can add the toggle onto my Click me link if the form is valid. 只是不确定如果表单有效,如何将切换按钮添加到“点击我”链接上。

Any help is welcomed. 欢迎任何帮助。

Thanks 谢谢

You can have a function called by the button when clicking, which will validate and expand the accordion accordingly. 单击时,您可以通过按钮调用一个函数,该函数将相应地验证和扩展手风琴。

To achieve this, the button would be: 为此,该按钮将是:

<a href="#" ng-click="validateAndSend()" class="btn mainbutton">Click me</a>

Then, you will have this function in your controller: 然后,您将在控制器中具有以下功能:

$scope.validateAndSend = function() {
    if (formIsValid() == true) {
        ctrl.toggle();
    }
}

Replace formIsValid() with your validation function and it should work. 用您的验证函数替换formIsValid() ,它应该可以工作。

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

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