我想创建一个动态表单,我想从 JSON 数据创建表单。我可以创建一个表单,但我想访问该动态创建的表单并在提交按钮上获取模型(输入字段)的值。 我为此创建了一个 fiddle ( https://jsfiddle.net/anujsphinx/dpzgeyt8/2/ )。想要获得每个字段的 ng-model 值。 我在 HTML 中做错了
<ion-view view-title="Search" ng-app="myAp">
<ion-content ng-controller="SearchCtrl">
<h1>Submit</h1>
<span class="input-label">Username</span>
<input type="text" ng-model="loginuser.use" name="text" placeholder="Your Username" required>
<span class="input-label">Email Id</span>
<input type="email" ng-model="loginuser.email" name="email" placeholder="Your Username" required>
<div data-ng-repeat="item in data track by $index">
<span class="input-label">{{item.name}} </span>
<input type="{{item.type}}" ng-model="newVar" name="email" placeholder="{{item.placeholder}}" required> {{newVar}}
</div>
<button type="button" ng-click="checkResult()">
Submit
</button>
</ion-content>
</ion-view>
最佳答案
这是获取所有表单元素值的方法。 添加新的表单元素并在表单中添加输入字段。
<form name="newForm">
... //input elements
</form>
添加一个循环以获取表单中所有元素的值
$scope.checkResult = function(){
var newForm = document.newForm;
for(i=0;i<newForm.elements.length;i++){
console.log(newForm.elements[i].value);
}
}
希望对你有帮助
关于javascript - Ionic 中的动态形式并获取 ng-model 的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40543101/