[英]Unable to get value of radio button in angular
I have these radio buttons but I can't get value from those on submit. 我有这些单选按钮,但是提交时无法获得任何价值。 I tried this exactly the same: http://plnkr.co/edit/4lvJclZuh0lryF1nhmdt?p=preview
我尝试了完全相同的方法: http : //plnkr.co/edit/4lvJclZuh0lryF1nhmdt?p=preview
HTML: HTML:
<body ng-controller="ExamCtrl">
<form name="myForm" ng-submit="submitForm()">
<label><input type="radio" name="test" ng-model="radioValue" value="1"/> One</label>
<label><input type="radio" name="test" ng-model="radioValue" value="2"/> Two</label>
<label><input type="radio" name="test" ng-model="radioValue" value="3"/> Three</label>
<div>currently selected: {{radioValue}}</div>
<button type="submit">Submit</button>
</form>
</body>
JS: JS:
$scope.submitForm = function () {
console.log('Submitting');
console.log($scope.radioValue);
};
It logs 'Submitting' and then undefined
. 它记录“ Submitting”,然后
undefined
。
It works when I use the dot '.' 当我使用点“。”时,它可以工作。 notation.
符号。 No idea why.
不知道为什么。
HTML HTML
<body ng-controller="ExamCtrl">
<form name="myForm">
<label><input type="radio" name="test" ng-model="radio.value" value="1"/> One</label>
<label><input type="radio" name="test" ng-model="radio.value" value="2"/> Two</label>
<label><input type="radio" name="test" ng-model="radio.alue" value="3"/> Three</label>
<div>currently selected: {{radioValue}}</div>
<button ng-click="submitAnswer(radio.value)">Submit</button>
</form>
</body>
JS JS
$scope.submitAnswer = function (selected) {
// $scope.answer = 'A';
console.log('submitting');
console.log(selected);
};
Your code is working fine : 您的代码工作正常:
Fiddle : https://jsfiddle.net/2scu8wL8/ 小提琴: https : //jsfiddle.net/2scu8wL8/
var myApp = angular.module('myApp',[]); myApp.controller('MyCtrl',function($scope) { $scope.submitForm = function () { console.log('Submitting'); console.log($scope.radioValue); }; });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script> <div ng-app="myApp" ng-controller="MyCtrl"> <form name="myForm" ng-submit="submitForm()"> <label><input type="radio" name="test" ng-model="radioValue" value="1"/> One</label> <label><input type="radio" name="test" ng-model="radioValue" value="2"/> Two</label> <label><input type="radio" name="test" ng-model="radioValue" value="3"/> Three</label> <div>currently selected: {{radioValue}}</div> <button type="submit">Submit</button> </form> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.