Get only unique names from more elements

我只是一个虾纸丫 提交于 2020-02-20 09:31:27

问题


I have dynamically rendered HTML that lists an undetermined number of radio buttons whose names represent some ids in a database.

I need to collect all of the unique names of the radios.

Here is an example:

<input name="721" type="radio" value="A" />        
<input name="721" type="radio" value="I" />     

<input name="722" type="radio" value="A" />        
<input name="722" type="radio" value="I" />     

<input name="723" type="radio" value="A" />        
<input name="723" type="radio" value="I" />     

Checking the radios is NOT required, so using a selected or checked attribute won't work.

Is there an elegant way to get all the unique names of the radios using jQuery? I know I can just loop through each of the radios, but I'm hoping that jQuery has a more elegant way to do this?


回答1:


jsBin demo

var arr = [];

$.each( $('input:radio'), function(){

  var myname= this.name;
  if( $.inArray( myname, arr ) < 0 ){
     arr.push(myname); 
  }

});

alert(arr); // 721,722,723



回答2:


var names = $('input[type=radio]').map(function() {
    return this.name
}).get(); // create an array of names

var unique = $.grep(names, function(v, i) {
    return $.inArray(v, names) === i
}); // filter unique values

http://jsfiddle.net/WnHvz/




回答3:


$('input:radio').each(function(){
    // do something
 });



回答4:


FYI - the html you posted doesnt have unique names. There's two of each... but this is how you can get them:

$('input[type="radio"]').each(function() {
    alert($(this).attr('name'));
});​



回答5:


if you want to print those names before each radio then use it:

$('input[type="radio"]').each(function() {
    $(this).before($(this).attr('name'));
});​

try out the fiddle: http://jsfiddle.net/teLjg/




回答6:


To get unique names, you can use map() to build an array of all the names, then combine $.grep() with $.inArray() to remove duplicates.

Something like:

var allNames = $("input[type=radio]").map(function() {
    return $(this).attr("name");
}).get();

var uniqueNames = $.grep(allNames, function(name, index) {
    return $.inArray(name, allNames) == index;
});



回答7:


Just try to loop thru and get all the names and store it in an array... then by using filter method you can filter the unique values....

let say,

var array=[your array values];

var unique_val = array.filter(function(value,iter,array){
    return iter == array.indexOf(value);
});

This might work...




回答8:


I think you people so fast and love jQuery, huh. this may help you, in case of 2 radio buttons, http://jsfiddle.net/BTcrR/ check this out

$(function() {
    // list of 
    var rediobuttonsName = new Array();
    // you can use :odd | :even
    $('input[type="radio"]').not(':odd').map(function(n, elem) {

       rediobuttonsName.push(elem.name);
       // or  you can use the context of object this
       // this.name

   });

   alert(rediobuttonsName);
});​

or you can get ride of the .not(":odd") and check the array if it has the value of the name. ;)



来源:https://stackoverflow.com/questions/14112804/get-only-unique-names-from-more-elements

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!