如何在angularjs中获取所选单选按钮的值

时间:2015-09-08 06:02:23

标签: angularjs

我有两个单选按钮passfail

如何获取所选单选按钮的值。

<div class="col-md-4">
            Result
            <div class="radio">
                <label><input type="radio" name="rdoResult">pass</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="rdoResult">fail</label>
            </div>
        </div>

我是否需要使用ng-model或其他东西。在jquery中,我很清楚angularjs中的事情。

7 个答案:

答案 0 :(得分:25)

两者都应该具有相同的Shape.Draw ng-value(用于选择ng-modeloption按钮),以便在{上更改所选值{1}} $ scope变量,您可以在表单提交或按钮单击时在控制器内获取该值。

<强>标记

radio

答案 1 :(得分:6)

&#13;
&#13;
angular.module('resultApp', []).controller('resultCtrl', function($scope) {
 
  $scope.result = 'pass';
  
  $scope.submitResult = function(result) {
    
    alert(result)
  };
});
&#13;
<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
  </head>
<body ng-app='resultApp' ng-controller='resultCtrl'>
  
  <div class="col-md-4">
    Result
    <div class="radio">
        <label><input type="radio" ng-model='result' ng-value='"pass"' name="rdoResult">pass</label>
    </div>
    <div class="radio">
        <label><input type="radio" ng-model='result' ng-value='"fail"' name="rdoResult">fail</label>
    </div>
</div>
  {{result}}
  <button ng-click="submitResult(result)">See Result</button>
  
  </body>
&#13;
&#13;
&#13;

答案 2 :(得分:3)

是的,您必须添加ng-model才能获得单选按钮的值

<div class="col-md-4">
                Result
                <div class="radio">
                    <label><input type="radio" ng-model="button.value" name="rdoResult" value="pass">pass</label>
                </div>
                <div class="radio">
                    <label><input type="radio" ng-model="button.value" name="rdoResult" value="fail">fail</label>
                </div>
            </div> {{button.value}}

答案 3 :(得分:0)

这是你的javascript

  <script>
    aap=angular.module('myApp',[])
    .controller('myCtrl',["$scope",function($scope){
      $scope.change='data';
      $scope.getVal=function(){

        console.log($scope.changedVal);
        $scope.change=$scope.changedVal;
      }
    }]);

  </script>

和你的HTML

<body ng-app="myApp">
    <h1>{{1+1}}</h1>
    <div class="col-md-4" ng-controller="myCtrl">
            Result
            <div class="radio">
                <label><input type="radio" name="rdoResult" ng-model="changedVal" value="pass" ng-click="getVal()">pass</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="rdoResult" ng-model="changedVal" value="fail" ng-click="getVal()">fail</label>
            </div>
            {{change}}
        </div></body>

working demo

希望这就是你要找的东西。

答案 4 :(得分:0)

指令内部和ng-repeat上述解决方案无效

以下是在指令中获取所选值的最佳方法:

Template : '<tr ng-repeat="item in ExistingReportData"><td class="text-center"><input type="radio" name="rbReportData" ng-change="RadioChange(item.ActivityHeaderId)" ng-model="SelectedExistingReportData" ng-value="{{item.ActivityHeaderId}}"/>{{item.ActivityHeaderId}}</td></tr>' 

controller: function ($scope) {
            $scope.RadioChange = function (s) {
                $scope.SelectedExistingReportData = s;
            };

            $scope.setWeldDataToGrid = function () {
                alert($scope.SelectedExistingReportData);
                //  $('.modal').modal('hide');
            }

        }



'<button class="btn button-main btn-primary btn-sm" id="btnWelderSetTo" ng-click="setWeldDataToGrid()" ><span aria-hidden="true" class="glyphicon glyphicon-save"></span> {{ \'OK\' | translate }}</button>'

答案 5 :(得分:0)

在ng-model中使用点(。)解决了问题:

在您的HTML中:

0.20

在您的控制器中:

<input id="base" type="radio" name="content" ng-model="radio.fileType" ng-value="'base'" ng-click="refreshScreen()">

<input id="dynamic" type="radio" name="content" ng-model="radio.fileType" ng-value="'dynamic'" ng-click="refreshScreen()">


<button type="button" class="btn btn-default" ng-click="downloadFile(radio.fileType)">Get Template</button>

答案 6 :(得分:0)

<input type="radio" ng-model="teamSelectionOption.name" value="choose">choose<br>
<input type="radio" ng-model="teamSelectionOption.name" value="create">create<br>

设置默认选项并更新它就像在控制器中一样

$scope.teamSelectionOption = {
  name: 'create'
};

然后在模板中访问。 {{teamSelectionOption.name}}