这两者有什么不同:

$.each($('#myTable input[name="deleteItem[]"]:checked').do_something());

这是:

$('#myTable input[name="deleteItem[]"]:checked').each(function() { do_something });

正在 Select 并执行操作的表格单元格的html如下所示:

<td width="20px"><input type="checkbox" class="chkDeleteItem" name="deleteItem[]" value="' . $rowItem['itemID'] . '" /></td>

I've gone over the jQuery documentation, but I still don't understand the difference. (Is it me or is that documentation sometimes slightly "nebulous" in clarity of content?)

Added Info:

Apparently my attempt a generic examples is confusing people! Along with the (previously) missing parenthesis in the first example. :(

The first example comes from a line in my code that removes the <tbody> for any rows with a checkbox that is checked:

$.each($('#classesTable input[name="deleteClasses[]"]:checked').parent().parent().parent().remove());

第二个示例来自于这样一种情况:我在#classesTable中查找任何选中的复选框,并在下拉列表中删除其匹配项.

$('#classesTable input[name="deleteClasses[]"]:checked').each(function(){
    $('#classesList option[value="' + $(this).attr('value') + '"]').remove();
});

I understand that they do two different things, but not to the point that I'd be able to say "I need to use $.each() in this case and .each(function() {}) in another case.

它们可以互换吗?只是在某些情况下?从不

推荐答案

Description:

.each是仅用于迭代jQuery的迭代器 对象集合,而jQuery.each($.each)是一般的 用于迭代JavaScript对象和数组的函数.


示例

1) Using $.each() function

var myArray = [10,20,30];

$.each( myArray, function(index, value) {
   console.log('element at index ' + index + ' is ' + value);
});

//Output
element at index 0 is 10
element at index 1 is 20
element at index 2 is 30

2) Using .each() method

$('#dv').children().each(function(index, element) {
    console.log('element at index ' + index + 'is ' + (this.tagName));
    console.log('current element as dom object:' + element);
    console.log('current element as jQuery object:' + $(this));
});

//Output
element at index 0 is input
element at index 1 is p
element at index 2 is span

资源

Jquery相关问答推荐

如何在没有实体框架的情况下在 ASP.NET 上使用 ajax 和 jquery 从列表创建数据表

5 秒后关闭 jQuery 弹出模式

Ajax 替换而不是追加

无法使用 HTML 设置未定义的 jQuery UI 自动完成的属性_renderItem

用 JS 触发 CSS 悬停

在 for 循环中分配点击处理程序

在 Javascript 中,字典理解或 Object `map`

如何使用 jQuery 获取 div 的当前类?

不使用插件的 jQuery 缓动函数

Rails,单击link_to helper后未加载javascript

用 jQuery 模拟按键

使用带有 HTML 表格的 jQuery UI 可排序

如何清除/重置 jQuery UI Datepicker 日历上的选定日期?

如果不是 jQuery,Javascript 中的美元符号是什么

更改占位符文本

计算文本宽度

将数据发布到 JsonP

Twitter Bootstrap 中的树

jQuery .val 更改不会更改输入值

jQuery / Javascript - 如何将像素值 (20px) 转换为数值 (20)