如果连续两次单击,Angularjs会重置无线电输入

时间:2015-07-06 19:55:08

标签: javascript jquery angularjs

我有以下jQuery点击事件,我正在尝试" angularize"使用 ng-click ,但无法弄清楚如何。

$('body').on('click change', 'input[type="radio"]:not([disabled])', function(event) {
    $(this).prop('checked') //if checked
    ? $(this).prop('checked',false).data('waschecked', false)   //uncheck
    : $(this).prop('checked',true).data('waschecked', true);    //else check
});

目标是检查项目是否确实已经被选中,如果已经选中,则重置单选按钮。

有人能指出我正确的方向吗?

2 个答案:

答案 0 :(得分:0)

这是非标准行为,但这是一个例子,可以帮助您查找:https://jsbin.com/sicuhugevu/1/edit?html,js,output

对于那些不想访问JSBin的人:

JS:

angular
  .module('test', [])
  .controller('testCtrl', function($scope) {
    $scope.radios = {
      selected: null,
      last: null
    };

    $scope.radioClick = function(event) {
      if ($scope.radios.last === $scope.radios.selected) {
        $scope.radios.selected = null;
        $scope.radios.last = null;

        return;
      }

      $scope.radios.last = event.target.value;
    };
});

HTML:

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body data-ng-app="test">
  <div data-ng-controller="testCtrl">
    <label><input type="radio" name="test" value="1"
                  data-ng-click="radioClick($event)"
                  data-ng-model="radios.selected">Option 1</label>
    <label><input type="radio" name="test" value="2"
                  data-ng-click="radioClick($event)"
                  data-ng-model="radios.selected">Option 2</label>
    <label><input type="radio" name="test" value="3"
                  data-ng-click="radioClick($event)"
                  data-ng-model="radios.selected">Option 3</label>
  </div>
</body>
</html>

本地版本:

&#13;
&#13;
angular
  .module('test', [])
  .controller('testCtrl', function($scope) {
    $scope.radios = {
      selected: null,
      last: null
    };
  
    $scope.radioClick = function(event) {
      if ($scope.radios.last === $scope.radios.selected) {
        $scope.radios.selected = null;
        $scope.radios.last = null;
        
        return;
      }
      
      $scope.radios.last = event.target.value;
    };
});
&#13;
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script>
  <meta charset="utf-8">
  <title>JS Bin</title>
</head>
<body data-ng-app="test">
  <div data-ng-controller="testCtrl">
    <label><input type="radio" name="test" value="1"
                  data-ng-click="radioClick($event)"
                  data-ng-model="radios.selected">Option 1</label>
    <label><input type="radio" name="test" value="2"
                  data-ng-click="radioClick($event)"
                  data-ng-model="radios.selected">Option 2</label>
    <label><input type="radio" name="test" value="3"
                  data-ng-click="radioClick($event)"
                  data-ng-model="radios.selected">Option 3</label>
  </div>
</body>
</html>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

我的建议是为组状态使用一个值,为按钮使用不同的值,并使用ng-click将按钮与组状态进行比较。

例如:

<input type="radio" ng-checked="true" ng-model="buttons" value="one" ng-click="radioChanged()">
<input type="radio" ng-checked="false" ng-model="buttons" value="two" ng-click="radioChanged()">
<input type="radio" ng-checked="false" ng-model="buttons" value="three" ng-click="radioChanged()">

$scope.radioChanged = function (){
    if ($scope.buttons === $scope.group){
        $scope.buttons = undefined;
    }
    $scope.group = $scope.buttons;
    console.log($scope.group, $scope.buttons);
};

http://plnkr.co/edit/lh9XjWr1cCxdf1asuKxo?p=preview