如何删除选中的单选按钮

use*_*953 1 html javascript jquery angularjs

小提琴.

这里我有一个表,在这个添加按钮就在那里,每次都会添加单选按钮,当我点击清除按钮时,应该取消选中所选的单选按钮.

$scope.Clear = function() {
  radiobtn = document.getElementById("theid");
  radiobtn.checked = false;  
};  
Run Code Online (Sandbox Code Playgroud)

Ank*_*wal 6

您的代码有几个问题

  • ng-clickclear按钮中缺少应该有的ng-click="Clear()"
  • 你不能拥有,ng-id = "theid"因为单选按钮是动态创建的,并且id为多个元素提供相同的值并不是一个好习惯
  • 然后,您可以使用JQuery重置该组中标记的单选按钮,但是当您使用ng-model获取单选按钮的值时,我建议您使用它ng-model来重置单选按钮值.

var app = angular.module('angularjs-starter', []);

  app.controller('MainCtrl', function($scope) {
  
  $scope.names = ['Mobile','Office','Home'];

  $scope.choices = [{id: 'choice1'}];
  
  $scope.addNewChoice = function() {
    var newItemNo = $scope.choices.length+1;
    $scope.choices.push({'id':'choice'+newItemNo});
  };
  
  
  $scope.Clear = function() {
    $('input[type="radio"]').attr('checked', false);
  };  
  
});
Run Code Online (Sandbox Code Playgroud)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="angularjs-starter" ng-controller="MainCtrl">
   <fieldset ng-model='y' ng-repeat="choice in choices">
      <select>
         <option ng-model='x1' ng-repeat = "x in names">{{x}}</option>
      </select>
      <input value="aa" type="radio" name="na"/>
   </fieldset>
   <button class="addfields" ng-click="addNewChoice()">Add fields</button>
    <button class="addfields" ng-click="Clear()">Clear</button>
</div>
Run Code Online (Sandbox Code Playgroud)