下拉列表和值

我在设置下拉列表的值时遇到问题。 在这里,我正在粘贴我的代码。

HTML代码:

<form ng-submit="save()"> <table style=" table-layout: fixed;" class="table"> <thead> <tr> <th>Section</th> <th>Names</th> <th>Marks</th> </tr> </thead> <tbody> <tr> <td>5th class</td> <td> <select id="5thclass" ng-model="setmarks.5thclass" ng-options="val for val in studentname"> <option ng-init="setmarks.5thclass=studentname[0]" selected="selected"></option> </select> </td> <td> <input type="number" name="5thclass" ng-model="setmarks.marks.5thclass"/> </td> </tr> </tbody> </table> <div class="sub" class="btn-group"> <input id="savebutton" type="submit" ng-click="Submit" value="Save" class="btn btn-primary"> </div><br /> </form> 

这里是我的angularjs代码:

  $scope.studentname = ["abc","pqr","xyz"]; $scope.save = function(){ $http.post('/savemarks', $scope.setmarks).success(function(data){ console.log("posted data successfully"); console.log( JSON.stringify(data)); }).error(function(data){ console.error("Error posting data"); }) } 

最后这里是我的express.js设置。 我在哪里保存表格的细节文件。

 app.post('/savemarks',urlencodedParser,function(req,res){ fs.appendFile(writecost_file,JSON.stringify(req.body,null,1),function(err){ if(err){ console.log(err); }else{ console.log("File saved"); } }); }); 

输出是JSON格式:

 {"5thclass":"abc","marks":{"5thclass":66}} 

我想输出应该是forms。

 {"5thclass":"abc","marks":{"abc":66}} 

在标记列中,我希望学生的名字,而不是类的名称。 谁能帮我 。 谢谢 。

您在html中的ng-model指令内使用了setmarks.marks.5thclass。 因为第五类被视为标记对象的属性。 这就是为什么它被称为"marks":{"5thclass":66} 。 保存事件被触发后,你必须在控制器中手动创buildjson对象。 如下。 希望这会对你有所帮助。

  var st = '{"5thclass" : "' + $scope.setmarks.5thclass + '","marks":{"' + $scope.setmarks.5thclass + '":' + $scope.setmarks.marks.5thclass + '} }'; var obj = JSON.parse(st);