AngularJs. Is it possible to deselect HTML “radio” input by click?

后端 未结 5 474
再見小時候
再見小時候 2020-12-10 12:55

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

This code:



        
相关标签:
5条回答
  • 2020-12-10 13:27

    You could do using below. set an attribute.

    public selectedMoiProfile : boolean  = false;
    
    <input type="radio" 
           name="selectMoi"
           [checked]="selectedProfile"
           [value]="selectedProfile"
           (click)="onSelect(moiProfile)"
           class="selectMoi">
    
    
     onSelect(p: profile) {
         p.selectedProfile= p.selectedProfile? false : true;
     }
    
    0 讨论(0)
  • 2020-12-10 13:42

    I solved the issue with the following code:

    ng-dblclick = "{{model}} = '' "
    
    0 讨论(0)
  • 2020-12-10 13:44

    A simple solution that works on Angular 1.3+ is:

    Template

    <input type="radio" ng-model="forms.selected" value="{{value}}" ng-click="radioCheckUncheck($event)">
    

    Controller

      let lastChecked = null
      $scope.radioCheckUncheck = function (event) {
        if (event.target.value === lastChecked) {
          delete $scope.forms.selected
          lastChecked = null
        } else {
          lastChecked = event.target.value
        }
      }
    

    It's similar to the above solution, but maintains its own copy of the previous selection.

    0 讨论(0)
  • 2020-12-10 13:50

    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)" />
    

    In your controller:

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

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

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

    0 讨论(0)
  • 2020-12-10 13:53

    A simpler solution is to add:

    ng-dblclick = "checked = null"
    
    0 讨论(0)
提交回复
热议问题