gpt4 book ai didi

javascript - 如何从表单标签之外的按钮手动触发 AngularJS 验证?

转载 作者:IT王子 更新时间:2023-10-29 03:22:52 25 4
gpt4 key购买 nike

给定这段代码:

<div ng-controller="MyCtrl">
<form ng-submit="onSubmitted()">

Header inputs:
<input type="name" ng-model="sample" required/>
<input type="name" ng-model="sampleX" required/>

<input type="submit" value="This submit triggers validation. But I wanted to put this button at the end of the page"/>
</form>

<hr/>

Some other form here. Think line items

<hr />
<a class="btn" ng-click="/* what could should be put here, so this can trigger the firt form's validation, then submit? */">Wanted this submit button to trigger the validation+submit on the form in which this button doesn't belong</a>


</div>


var app = angular.module('myApp', []);

function MyCtrl($scope) {

$scope.onSubmitted = function() {
alert('submitted!');
};
}

我希望最后一个按钮在第一个表单上触发验证(然后在有效时提交)。截至目前,只有表单内的按钮可以触发该表单的验证和提交。表单外的按钮是否有任何可能的方法来做到这一点?

现场测试:http://jsfiddle.net/dzjV4/1/

最佳答案

您可以创建指令,然后附加到 <a class="btn"... .检查这个 jsfiddle

http://jsfiddle.net/dzjV4/2/

请注意,我添加到 <input type='submit' id='clickMe'...并将其链接到底部的链接 <a class='btn' linked="clickMe"...

关于javascript - 如何从表单标签之外的按钮手动触发 AngularJS 验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11920482/

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