在 JQuery 中,如何访问 DOM 元素数组?
正在编写一些使用 jQuery MultiSelect 小部件的继承代码。我有一个 jQuery 问题(因此将其作为楔 block 制造商的问题是不公平的)。
这个对象的文档和演示非常好。在此页面上,http://www.erichynds.com/examples/jquery-ui-multiselect-widget/demos/#selectedlist它有一些 JavaScript 代码:
$("select").multiselect({
selectedText: function(numChecked, numTotal, checkedItems){
return numChecked + ' of ' + numTotal + ' checked';
}
});
我感兴趣的是“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.
我想修改此函数,以便对此数组的每个成员checkedItems 执行字符串操作。我该怎么做?
我希望我已经提供了足够的信息来为 JQuery 专家确定答案。我想我有。
最佳答案
使用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
});
文档
jQuery.each
- http://api.jquery.com/jQuery.each/
关于javascript - 在 JQuery 中,如何访问 DOM 元素数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18498530/