如何获取给定表单详细信息的嵌套json对象?

时间:2016-04-06 06:53:30

标签: javascript jquery angularjs json angularjs-ng-model

我正在尝试获取json对象而我已经尝试了它但是它以简单的json格式出现,但我需要它像{I}格式那样我所解释的在fiddle。我怎样才能实现它?请帮助我,并提前感谢。

2 个答案:

答案 0 :(得分:1)

$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()

}

使用此代码。

答案 1 :(得分:1)

替换你的表单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"