如何获取给定表单详细信息的嵌套 json 对象?
How to get nested json object for given form details?
我正在尝试获取 json
对象,我已经尝试过了,但它以普通的 json 格式出现,但我需要它,就像 nested json
格式一样我在fiddle里解释了。我怎样才能实现它?请帮助我并提前致谢。
$scope.serialize = function($event){
var testing = [
{'Fname':$scope.formData.Fname},
{'Lname':$scope.formData.Lname},
{'Mname':$scope.formData.Mname},
{'Education':$scope.formData.Education},
{'Age':$scope.formData.Age},
];
$scope.formData = {
'testing' : testing,
'University' : $scope.formData.University,
'Companies' : $scope.formData.companies,
};
console.log($scope.formData)
alert(JSON.stringify($scope.formData))
console.log(JSON.stringify($scope.formData));
$event.preventDefault()
}
使用此代码。
将您的表单 html 替换为
<form action="" method="post" id="formid" name="testForm">
First Name:
<input type="text" ng-model="formData.testing.Fname" maxlength="50" size="12" /><br/>
<br/> Last Name:
<input type="text" ng-model="formData.testing.Lname" maxlength="50" size="12" /><br/>
<br/>
Middle Name:
<input type="text" ng-model="formData.testing.Mname" maxlength="50" size="12" /><br/>
<br/> Education:
<br/>
<select ng-model="formData.testing.Education">
<option value="HighSchool">HighSchool</option>
<option value="College">College</option>
</select>
<br/>
<br/> Age:
<input type="text" ng-model="formData.testing.Age" maxlength="2" size="10" /><br/>
<br/> University:
<br/>
<select ng-model="formData.University">
<option value="ABC">ABC</option>
<option value="DEF">DEF</option>
</select>
<br/>
<br/>
Companies:<br/>
<select ng-model="formData.companies">
<option value="X">X</option>
<option value="Y">Y</option>
<option value="Z">Z</option></select><br/>
<p>
<input type="submit" ng-click="serialize($event)" />
</p>
</form>
这意味着您在测试节点中想要的数据在绑定时以相同的格式放置,即将 ng-model="formData.Fname"
替换为 ng-model="formData.testing.Fname"
我正在尝试获取 json
对象,我已经尝试过了,但它以普通的 json 格式出现,但我需要它,就像 nested json
格式一样我在fiddle里解释了。我怎样才能实现它?请帮助我并提前致谢。
$scope.serialize = function($event){
var testing = [
{'Fname':$scope.formData.Fname},
{'Lname':$scope.formData.Lname},
{'Mname':$scope.formData.Mname},
{'Education':$scope.formData.Education},
{'Age':$scope.formData.Age},
];
$scope.formData = {
'testing' : testing,
'University' : $scope.formData.University,
'Companies' : $scope.formData.companies,
};
console.log($scope.formData)
alert(JSON.stringify($scope.formData))
console.log(JSON.stringify($scope.formData));
$event.preventDefault()
}
使用此代码。
将您的表单 html 替换为
<form action="" method="post" id="formid" name="testForm">
First Name:
<input type="text" ng-model="formData.testing.Fname" maxlength="50" size="12" /><br/>
<br/> Last Name:
<input type="text" ng-model="formData.testing.Lname" maxlength="50" size="12" /><br/>
<br/>
Middle Name:
<input type="text" ng-model="formData.testing.Mname" maxlength="50" size="12" /><br/>
<br/> Education:
<br/>
<select ng-model="formData.testing.Education">
<option value="HighSchool">HighSchool</option>
<option value="College">College</option>
</select>
<br/>
<br/> Age:
<input type="text" ng-model="formData.testing.Age" maxlength="2" size="10" /><br/>
<br/> University:
<br/>
<select ng-model="formData.University">
<option value="ABC">ABC</option>
<option value="DEF">DEF</option>
</select>
<br/>
<br/>
Companies:<br/>
<select ng-model="formData.companies">
<option value="X">X</option>
<option value="Y">Y</option>
<option value="Z">Z</option></select><br/>
<p>
<input type="submit" ng-click="serialize($event)" />
</p>
</form>
这意味着您在测试节点中想要的数据在绑定时以相同的格式放置,即将 ng-model="formData.Fname"
替换为 ng-model="formData.testing.Fname"