如何在角度js中将选中的单选按钮值发送到服务器

How to send the checked radio button value to Server in angular js

本文关键字:单选按钮 服务器 js      更新时间:2023-09-26

我想将选中的单选按钮值以角度发送到服务器

我试过了,但我收到 500 服务器错误

当我只发送有效的问题 ID 时

这是 CreateController 的代码

var CreateController = ['$scope', '$http', function ($scope, $http) {
    $scope.mydata = 'ajs works';
    $scope.model = {
        Questions:{}
    };
    $http.get('/Testing/IndexVM').success(function (newdata) {
        $scope.model = newdata;
    });
    $scope.NextQuestion = function () {
        $http.post('/Testing/NextQuestion', {
            quid: $scope.model.Questions.AddQuestionID, selectedopt: $scope.model.Questions.Options.OptionID
        }).success(function (newdata) {
            $scope.model = newdata;
        }); 
}];

这是索引视图的代码,我要发送的内容

    <div ng-controller="CreateController">
<tr>
            <td>
                <h3>Question: {{model.Questions.Question}}</h3>
            </td>
        </tr>
     <tr ng-repeat="item in model.Questions.Options" ng-model="model.Questions.Options.OptionID">
                <td class="switch radius">
                    <input id="{{item.Options}}" type="radio" name="selectedvalue" value="{{item.Options}}">
                    <label for="{{item.Options}}"></label>{{item.Options}}
                </td>
            </tr>
     </div>

ng 模型移动到输入元素,以便每个单选按钮具有相同的 ng 模型和相同的名称属性,而不是具有单选按钮组并且模型具有所选值属性的值

所以 ng-model="model。Questions.Options.OptionID"不是"item"

在单选按钮中添加 ng-model:

<input id="{{item.Options}}" type="radio" name="selectedvalue" value="{{item.Options}} ng-model="item">