gpt4 book ai didi

javascript - AngularJS 获取没有表单的无效元素组

转载 作者:行者123 更新时间:2023-12-03 10:16:16 25 4
gpt4 key购买 nike

有没有一种好的方法可以在不使用表单的情况下获取一组元素?我的问题是,使用 .net mvc 和 Html.BeginForm 会删除其内部的任何表单。

<fieldset name="user_fieldset">
<input type="text" ng-model="Firstname" />
<input type="text" ng-model="Lastname" />
...
</fieldset>
<button ng-disabled="user_fieldset.$valid" ng-class="{'disabled' : user_fieldset.$valid}">Next</button>

最佳答案

您可以使用ng-form指令作为属性。

来自 AngularJS Hub 的示例:

angular.module("mainModule", [])
.controller("mainController", function ($scope)
{
$scope.person = {};
});
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<style>
label
{
display: inline-block;
width: 120px;
vertical-align: middle;
}

input
{
display: inline-block;
vertical-align: middle;
}

input.ng-invalid
{
border: solid red 2px;
}

form
{
padding: 10px;
}

textarea
{
width: 250px;
height: 160px;
}
</style>
</head>

<body ng-app="mainModule">
<div ng-controller="mainController">
<form name="personForm" novalidate>
<label for="firstNameEdit">First name:</label>
<input id="firstNameEdit" type="text" name="firstName" ng-model="person.firstName" required /><br />
<label for="lastNameEdit">Last name:</label>
<input id="lastNameEdit" type="text" name="lastName" ng-model="person.lastName" required /><br />
<ng-form name="addressForm">
<label for="addressEdit">Address:</label>
<input id="addressEdit" type="text" name="address" ng-model="person.address" /><br />
<label for="zipEdit">ZIP code:</label>
<input id="zipEdit" type="number" name="zip" ng-model="person.zip" required /><br />
<label for="cityEdit">City:</label>
<input id="cityEdit" type="text" name="city" ng-model="person.city" /><br />
<label for="stateProvinceEdit">State/Province:</label>
<input id="stateProvinceEdit" type="text" name="stateProvince" ng-model="person.stateProvince" /><br />
<label for="countryEdit">Country:</label>
<input id="countryEdit" type="text" name="country" ng-model="person.country" />
</ng-form>
</form>
<br />
<strong><label for="userDebugText">Person:</label></strong><br />
<textarea id="userDebugText">{{person | json}}</textarea><br />
<br />
<strong>Form validity:</strong><br />
<ul>
<li><strong>personForm.$valid =</strong> {{personForm.$valid}}</li>
<li><strong>personForm.addressForm.$valid =</strong> {{personForm.addressForm.$valid}}</li>
<li><strong>personForm.firstName.$valid =</strong> {{personForm.firstName.$valid}}</li>
<li><strong>personForm.lastName.$valid =</strong> {{personForm.lastName.$valid}}</li>
<li><strong>personForm.addressForm.zip.$valid =</strong> {{personForm.addressForm.zip.$valid}}</li>
</ul>
</div>
</body>
</html>

关于javascript - AngularJS 获取没有表单的无效元素组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29856573/

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