简体   繁体   English

removeAttr不适用于angularjs

[英]removeAttr does not work with angularjs

I am trying to remove the checked attribute from a set of radio buttons. 我试图从一组单选按钮中删除选中的属性。 I am using angularjs but I have tried with jquery and it is the same. 我正在使用angularjs,但是我尝试使用jquery,它是相同的。 Here is my code with angularjs: 这是我的angularjs代码:

 var radioList = angular.element(document.querySelectorAll(".radioButtons"));
        for (var i = 0; i <= radioList.length; i++) {
            radioList[i].removeAttr('checked');
        }

The error is: 错误是:

TypeError: radioList[i].removeAttr is not a function

what am I doing wrong?? 我究竟做错了什么?? Thanks in advance. 提前致谢。

UPDATE: Angular version: AngularJS v1.5.8 更新:Angular版本:AngularJS v1.5.8

Your are performing 1 extra loop your loop condition should be i< instead of i<= should be like 您正在执行1个额外循环,您的循环条件应为i<而不是i<=应该像

for (var i = 0; i < radioList.length; i++) {
            radioList[i].checked=true;
        }

Here is working demo 这是工作演示

First things first, angularJS makes use of jQuery internally so you can feel free to work with element selectors as you would do in jQuery. 首先,angularJS在内部使用jQuery,因此您可以像在jQuery中一样随意使用元素选择器。 After calling the angular.element function, either you're returned no, one or multiple results you'll be returned a results array. 调用angular.element函数后,返回的结果为no,一个或多个结果,将返回结果数组。 As this array is jQuery-aware, you can call a jQuery function on its reference which will be called for every entry in the returned results array. 由于此数组支持jQuery,因此您可以在其引用上调用jQuery函数,该函数将为返回的结果数组中的每个条目调用。 So simply you can do this instead; 因此,您可以简单地做到这一点;

angular.element('.radioButtons').removeAttr('checked');

I solved the problem adding a value into my input. 我解决了在输入中添加值的问题。

<input type="radio" name="presetRadio" id="{{filterData.id}}"
       ng-model="currentRule.filters"
       value="{{filterData.id}}"          
       ng-checked="filterData.active" />

Thanks to all!! 谢谢大家!!

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

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