【Angular】radio选项如何确定是否选中,唯一性选中

来源:互联网 发布:故宫 淘宝 彩妆 编辑:程序博客网 时间:2024/06/04 18:21

在单选题的4个选项中,要保证1个为true,其它3个选项为false;这里用的radio属性;

<input type='radio'>

radio是收音机的意思,老式收音机,按下一个按钮后,其它的按钮都会弹上来,所以很形象的用radio来作为type的值;表示唯一性,排他性选中;

但是在我的页面,只要我鼠标点击过的选项,都会被设置成true;就是说,虽然,浏览器页面看到的是,一个选中,其它三个没选中,但是在js逻辑层,只要选中过(注意过字!!)的,都会变成true;这里讲一下手动处理方法,处理方法:

1、【浏览器显示页面】:
这里写图片描述
但是js逻辑里,实际上不是这样的,只要我点击过的选项都是true;这里写了一个点击事件;

【源代码】:

<label class="m-b-none" ng-if="q.type==='radio'||q.type==='tf'">    <input type="radio"  id="radio" name="choices" ng-model="choice.correct" ng-checked="checked" ng-click="getExclusiveness($index)" ng-value="true"> 正确</label>

2、【html页面】:
这里写图片描述

3、【js页面代码】:
这里写图片描述

【源代码】:

    $scope.getExclusiveness=function($index) {        if ('radio'===$scope.q.type) {            var cbk = document.getElementById('radio');            console.log(cbk);            console.log($scope.q.choices);            console.log("$index:" + $index);                //把选中项设置为true,把其它项设置为false;                $scope.q.choices[$index].correct =true;                for (var i=0;i<$scope.q.choices.length;i++) {                    if (i != $index) {                        $scope.q.choices[i].correct = false;                        console.log("i:" + i);                        console.log($scope.q.choices[i].correct);                    }                }                alert('复选框将选中!');        }else if('check'===$scope.q.type) {        }    };

4、【浏览器控制台】
打印信息,打印内容跟js页面:

这里写图片描述