简体   繁体   English

在JQuery中,如何访问DOM元素数组?

[英]In JQuery, how do I access an array of DOM elements?

In JQuery, how do I access an array of DOM elements? 在JQuery中,如何访问DOM元素数组?

am coding some inherited code that uses the jQuery MultiSelect widget. 我正在编写一些使用jQuery MultiSelect小部件的继承代码。 I have a question that is a jQuery question (so no fair brushing this as a quesiton for the wedget maker). 我有一个问题是一个jQuery问题(所以没有公平地将其作为楔形制作者的问题)。

The documentation and demos for this object is pretty good. 该对象的文档和演示非常好。 On this page, http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/#selectedlist it has a bit of javascript code: 在这个页面上, http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/#selectedlist它有一些javascript代码:

$("select").multiselect({
selectedText: function(numChecked, numTotal, checkedItems){
    return numChecked + ' of ' + numTotal + ' checked';
}

}); });

What I am interested in is "checkedItems". 我感兴趣的是“checkedItems”。 The documentation says: 文件说:

The function receives three arguments: the number of checkboxes checked, the total number of checkboxes, and an array of the checkboxes (DOM elements) that were checked. 该函数接收三个参数:选中的复选框的数量,复选框的总数以及已检查的复选框(DOM元素)的数组。

I want to modify this function so that I perform string operations on each member of this array, checkedItems. 我想修改这个函数,以便我对这个数组的每个成员,checkedItems执行字符串操作。 How would I do this? 我该怎么做?

I hope I have provided enough information to determine an answer for a JQuery expert. 我希望我已经提供了足够的信息来确定JQuery专家的答案。 I think I have. 我想我有。

Use jQuery.each to iterate over the array: 使用jQuery.each迭代数组:

$.each(checkedItems, function (idx, ele){
    // ele is a reference to the individual element, so:
    console.log(ele.value); // outputs the value of the checkbox
});

Documentation 文档

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

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