gpt4 book ai didi

javascript - 单击按钮时获取单选按钮值不起作用angularjs

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:14:31 24 4
gpt4 key购买 nike

我是 angularjs 的新手。我想在用户单击按钮时获取单选按钮的值,但我运气不好,它不起作用。这是代码

<label class="item item-radio">
<input type="radio" name="group" ng-model="user.answer" value="{{questions.quiz_ans_opt1}}">
<div class="item-content item-body">a) {{questions.quiz_ans_opt1}}</div><i class="radio-icon ion-checkmark"></i>
</label>
<label class="item item-radio item-body">
<input type="radio" name="group" ng-model="user.answer" value="{{questions.quiz_ans_opt2}}">
<div class="item-content item-body">b) {{questions.quiz_ans_opt2}}</div><i class="radio-icon ion-checkmark"></i>
</label>
<button class="button button-balanced button-block" ng-click="submitAnswer(user)">Submit</button>

这是 Controller

$scope.submitAnswer = function(user) {
//it output undefined error
alert(user);
}

我还想在选中单选按钮之前禁用该按钮,我该如何实现?

最佳答案

请看这个,

试试这个,

在 html 中,

<body ng-controller="MainCtrl">
<p>Hello {{name}}!</p>
<input type="radio" ng-model="user.answer" ng-value="'option a'">
<label>option a</label>
<br>
<input type="radio" ng-model="user.answer" ng-value="'option b'">
<label>option b</label>
<br>
<input type="radio" ng-model="user.answer" ng-value="'option c'">
<label>option c</label>
<br>
<input type="radio" ng-model="user.answer" ng-value="'option d'">
<label>option d</label>
<br>
<button ng-disabled="!user.answer" ng-click="submitAnswer(user)">Submit</button>
</body>

在 app.js 中,

var app = angular.module('plunker', []);

app.controller('MainCtrl', function($scope) {
$scope.name = 'World';
$scope.submitAnswer=function(user){

alert(user.answer);

}
});

这是 plunker相同的演示

关于javascript - 单击按钮时获取单选按钮值不起作用angularjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26828869/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com