AngularJs。是否有可能通过点击取消HTML“单选”输入? [英] AngularJs. Is it possible to deselect HTML “radio” input by click?

查看:348
本文介绍了AngularJs。是否有可能通过点击取消HTML“单选”输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有无线电输入和希望,如果目前的电台被选中在电台通过点击ucheck状态。

I have radio inputs and want to ucheck state by click on radio if current radio is checked.

这code:

<input type="radio" id="average_0" name="average" ng-model="checked" ng-change="false" value="500" class="ng-valid ng-dirty">
<input type="radio" id="average_1" name="average" ng-model="checked" ng-change="false" value="1000" class="ng-valid ng-dirty">
<input type="radio" id="average_2" name="average" ng-model="checked" ng-change="false" value="1500" class="ng-valid ng-dirty">

不工作。

小提琴: http://jsfiddle.net/Zoomer/8s4m2e5e/

推荐答案

单选按钮只能一次选定的,并且不能由用户一旦被检查取消选择(除非你做编程)。所以如果你想当前选择它来取消它,你可以这样做:

Radio buttons can be selected only one at a time and cannot be unchecked by the user once they are checked (unless you do programmatically). So if you want to uncheck it when it is currently selected, you can do this:

<input type="radio" ng-model="checked" value="500" ng-click="uncheck($event)" />
<input type="radio" ng-model="checked" value="1000" ng-click="uncheck($event)" />
<input type="radio" ng-model="checked" value="1500" ng-click="uncheck($event)" />

在你的控制器:

$scope.uncheck = function (event) {
    if ($scope.checked == event.target.value)
        $scope.checked = false
}

DEMO: http://jsfiddle.net/8s4m2e5e/3/

请注意:如果你真的想选择从众多选项中的一个或没有,你可以选择一个&LT;选择&GT;

NOTE: If you really want to choose one or none from many options, you may opt for a <select>

这篇关于AngularJs。是否有可能通过点击取消HTML“单选”输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆