繁体   English   中英

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

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

我有以下HTML:

单选按钮列表:

<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"/>

复选框列表:

<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"/>

和以下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>

如何获得jQuery中类的不同元素名称?

所需结果:

["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"]

小提琴

简单地说,该解决方案映射的元素的名称到被一个jQuery包装对象阵列迭代时和独特的值被到一个数组。


映射不是必须的,因此为简单起见,您也可以跳过该步骤。

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

小提琴

您可以执行以下操作:

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

码:

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

我相信你想要的是

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);

首先,我们将dom元素的集合映射到具有所述元素名称的字符串值的集合。 其次,我们过滤此名称集合以仅包含唯一实例。

注意:其他答案建议使用jQuery.unique。 但是,此方法适用于dom元素的集合,在您的情况下,所有这些都是唯一的。

类似于filter()

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

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

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

您可以使用jQuery .unique()方法,

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

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

尝试这个

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

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

暂无
暂无

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

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