简体   繁体   English

AngularJS - 取消选中单选按钮

[英]AngularJS - uncheck radio button

The following JSFiddle allows me to deselect a radio button selection.以下 JSFiddle 允许我取消选择单选按钮选择。

http://jsfiddle.net/8s4m2e5e/132/ http://jsfiddle.net/8s4m2e5e/132/

I'm using AngularJS 1.4 and in one of the posts here, I read that the above JSFiddle doesn't work with angular 1.3+ versions.我正在使用 AngularJS 1.4,在其中一篇文章中,我读到上述 JSFiddle 不适用于 Angular 1.3+ 版本。 Thats true, it doesn't work when I implemented the code in the above fiddle.是的,当我在上面的小提琴中实现代码时它不起作用。 How do I get this functionality to work with my version?如何让此功能与我的版本一起使用?

Following is my code.以下是我的代码。

foreach (var part in Model.ChildParts)
{
  <div class="radio  col-md-12">
    <input type="radio"
           id="@Model.FieldName"
           name="@Model.FieldName"
           ng-model="gPA(@Model.Id).value"
           value="@((part as BaseInputPartVM).GetStringValue())"
           ng-click="uncheck($event, @Model.Id)" />
  </div>
 }

In my controller,在我的控制器中,

$scope.uncheck = function (event, partId) {
      if ($scope.gPA(partId).value == event.target.value)
          $scope.gPA(partId).value = false
}

This code works for deselecting (uncheck) a radio button selection.此代码适用于取消选择(取消选中)单选按钮选择。 However, the basic radio button selection doesn't work and I'm not able to select any radios, because the above if condition is always turning to true.但是,基本的单选按钮选择不起作用,我无法选择任何单选,因为上述 if 条件总是变为 true。

Is there a way I can check for the Previously selected radio value and the new selected value?有没有办法检查以前选择的无线电值和新选择的值? In order to get the above functionality to work correctly, I need to access both the previous and new radio selected values for comparison.为了使上述功能正常工作,我需要访问以前的和新的无线电选择值进行比较。 I've looked into ng-model, ng-change and all of them fetches the current radio selected value, but I also need to know the previous selected value as well.我已经研究了 ng-model、ng-change,它们都获取了当前无线电选择的值,但我还需要知道以前选择的值。

Okay.好的。 I think I got an answer to my question... for the input radio, I added ng-mouseup event to capture the previous value of the radio selection before the click.我想我得到了我的问题的答案......对于输入收音机,我添加了 ng-mouseup 事件以在单击之前捕获收音机选择的先前值。 Also, I've added a hidden variable on the page to store this previous radio button selected value.另外,我在页面上添加了一个隐藏变量来存储这个以前的单选按钮选择的值。 Then in the ng-click event, I will check the previous selected value against the current selected value.然后在 ng-click 事件中,我将根据当前选择的值检查先前选择的值。 If previous and current values are same, I will set the value to null or false, to uncheck the selection.如果以前的值和当前的值相同,我会将值设置为 null 或 false,以取消选中该选择。

foreach (var part in Model.ChildParts)
{
  <div class="radio  col-md-12">
    <input type="radio"
           id="@Model.FieldName"
           name="@Model.FieldName"
           ng-model="gPA(@Model.Id).value"
           value="@((part as BaseInputPartVM).GetStringValue())"
           ng-mouseup = "setPreviousSelectedValue(@Model.Id)"
           ng-click="uncheck($event, @Model.Id)" />
  </div>
}
<input type="hidden" id="previousRadioSelection" name="previousRadioSelection" ng-model="previousRadioSelection" value="" />

In the controller,在控制器中,

//set previous selected value of radio button      
$scope.setPreviousSelectedValue = function (partId) {
    $scope.previousRadioSelection = $scope.gPA(partId).value;
}

//uncheck radio button      
$scope.uncheck = function (event, partId) {
    if ($scope.previousRadioSelection == event.target.value)
        $scope.gPA(partId).value = null;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM