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

时间:2022-04-13 23:54:40

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

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

am coding some inherited code that uses the jQuery MultiSelect widget. I have a question that is a jQuery question (so no fair brushing this as a quesiton for the wedget maker).

我正在编写一些使用jQuery MultiSelect小部件的继承代码。我有一个问题是一个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". The documentation says:

我感兴趣的是“checkedItems”。文件说:

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. How would I do this?

我想修改这个函数,以便我对这个数组的每个成员,checkedItems执行字符串操作。我该怎么办?

I hope I have provided enough information to determine an answer for a JQuery expert. I think I have.

我希望我已经提供了足够的信息来确定JQuery专家的答案。我想我有。

1 个解决方案

#1


2  

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

#1


2  

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