简体   繁体   English

使用jQuery获取元素的不同名称

[英]Get Distinct name of elements with a class using jquery

I have the following HTML: 我有以下HTML:

Radio button list(s): 单选按钮列表:

<input name="rdLst163" class="required_Mandatory_RbtLst" id="rdLst163_1" onclick="fn("1631");" type="radio" value="1"/>
<input name="rdLst163" class="required_Mandatory_RbtLst" id="rdLst163_2" onclick="fn("1632");" type="radio" value="2"/>
<input name="rdLst163" class="required_Mandatory_RbtLst" id="rdLst163_3" onclick="fn("1633");" type="radio" value="3"/>

<input name="rdLst164" class="required_Mandatory_RbtLst" id="rdLst164_1" onclick="fn("1641");" type="radio" value="1"/>
<input name="rdLst164" class="required_Mandatory_RbtLst" id="rdLst164_2" onclick="fn("1642");" type="radio" value="2"/>
<input name="rdLst164" class="required_Mandatory_RbtLst" id="rdLst164_3" onclick="fn("1643");" type="radio" value="3"/>

Checkbox list(s): 复选框列表:

<input name="chLst165" class="required_Mandatory_ChkLst" id="chkLst165_1" onclick="fn("25");" type="checkbox" value="1"/>
<input name="chLst165" class="required_Mandatory_ChkLst" id="chkLst165_2" onclick="fn("25");" type="checkbox" value="2"/>
<input name="chLst165" class="required_Mandatory_ChkLst" id="chkLst165_3" onclick="fn("25");" type="checkbox" value="3"/>

<input name="chLst166" class="required_Mandatory_ChkLst" id="chkLst166_1" onclick="fn("25");" type="checkbox" value="1"/>
<input name="chLst166" class="required_Mandatory_ChkLst" id="chkLst166_2" onclick="fn("25");" type="checkbox" value="2"/>
<input name="chLst166" class="required_Mandatory_ChkLst" id="chkLst166_3" onclick="fn("25");" type="checkbox" value="3"/>

and the following jQuery : 和以下jQuery:

<script>
$(document).ready(function () {
  $('.required_Mandatory_ChkLst[name!=]'); 
  $('.required_Mandatory_RbtLst[name!=]');
// result is fetching all 6 elements. I need only distinct element names [here 2 elements] in each class.
});
</script>

how can I get distinct element names of class in jQuery? 如何获得jQuery中类的不同元素名称?

Required result: 所需结果:

["rdLst163", "rdLst164"]  
["chLst165", "chLst166"]
var cbnames = [];
$('.required_Mandatory_ChkLst[name!=]').map(function() {
    return this.name;
}).each(function(i, str) {
    if (!~$.inArray(str, cbnames)) cbnames.push(str);
});
console.log(cbnames); //["chLst165", "chLst166"]

Fiddle 小提琴

Simply put, this solution maps the elements' names into a jQuery object-wrapped array which is iterated upon and the unique values are pushed into an array. 简单地说,该解决方案映射的元素的名称到被一个jQuery包装对象阵列迭代时和独特的值被到一个数组。


Mapping is not quite necessary, so for simplicity you could also do it skipping that step. 映射不是必须的,因此为简单起见,您也可以跳过该步骤。

var cbnames = [];
$('.required_Mandatory_ChkLst[name!=]').each(function() {
    if (!~$.inArray(this.name, cbnames)) cbnames.push(this.name);
});
console.log(cbnames); //["chLst165", "chLst166"]

Fiddle 小提琴

You can do something like: 您可以执行以下操作:

Demo : http://jsfiddle.net/DvegQ/ 演示http : //jsfiddle.net/DvegQ/

Code: 码:

var classNames = [];
$('*').each(function() { classNames[$(this).attr('name')] = 1; });
for(var x in classNames) { console.log(x); }

I believe what you want is this 我相信你想要的是

var mapped = $('.required_Mandatory_ChkLst[name!=], .required_Mandatory_RbtLst[name!=]').map(function() {
    return $(this).attr('name');
});

var unique = mapped.filter(function (i, itm) {
    return i == $.makeArray(mapped).indexOf(itm);
});

console.log(unique);

First we map the collection of dom elements to that of string values with the name of said element. 首先,我们将dom元素的集合映射到具有所述元素名称的字符串值的集合。 Secondly we filter this collection of names to only contain unique instances. 其次,我们过滤此名称集合以仅包含唯一实例。

Note: Other answers suggest the use of jQuery.unique. 注意:其他答案建议使用jQuery.unique。 This method however works on collections of dom elements and in your case all of these are unique. 但是,此方法适用于dom元素的集合,在您的情况下,所有这些都是唯一的。

Similar to filter() : 类似于filter()

$(function(){
    var names = [];
    $('.required_Mandatory_RbtLst').each(function(){

        if($.inArray($(this).prop('name'), names) === -1)
            names.push($(this).prop('name'));
    });

    console.log($(names));
});​

You can use the jQuery .unique() method, 您可以使用jQuery .unique()方法,

jQuery.unique($('.required_Mandatory_RbtLst[name!=]'));

REF: http://api.jquery.com/jQuery.unique/ REF: http : //api.jquery.com/jQuery.unique/

Try this 尝试这个

var names = $("input").map(function(){ return $(this).attr('name'); });
var distinctNames = $.unique(names);

read more about the map function here http://jqapi.com/#p=map 在此处阅读有关map函数的更多信息http://jqapi.com/#p=map

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

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