如何以angularjs形式获得多个单选按钮值?

时间:2014-09-20 12:19:34

标签: angularjs angularjs-ng-repeat

我有提问者表格,在他们的问题中有多种选择。如何获得用户选择的选项?

这是代码?

         ...
         <ul>
            <li ng-repeat="question in questionPaper.questions">
              <div>{{question.description}}</div>
              <ul>
                <li ng-repeat="option in question.options">
                  <span>
                <input type="radio" name="option_question_{{question.id}}" ng-model="option.id"/>
                {{option.description}}
              </span>
                </li>
              </ul>
            </li>
          </ul>
         ...

如何将选项值设为

{question_paper_id: 1, answers: [{question_id: 1, options_id: 23}, {question_id: 2, options_id: 21}, {..}] }   

这是我在plnkr http://plnkr.co/edit/OTDsUUCiDGYfxJ2AMzFR

中的代码

2 个答案:

答案 0 :(得分:1)

基本上,您可以在选择单选按钮时动态添加属性:

<input type="radio" ng-model="question.selected_id" ng-value="option.id"/>

$scope.selected_ids = [];
$scope.submitAnswers = function() {
    angular.forEach($scope.questionPaper.questions, function(question) {
        $scope.selected_ids.push(question.selected_id);
    });
}

看看演示:http://plnkr.co/edit/QG8XP0jfjyJPY1xaXx6d?p=preview

答案 1 :(得分:0)

其他一切似乎都很好。只需将您的脚本标记替换为包含此

的角度
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.22/angular.min.js"></script>