在jquery对象的集合上使用underscore.js列表函数 [英] use underscore.js list functions on collection of jquery objects

查看:64
本文介绍了在jquery对象的集合上使用underscore.js列表函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用同时使用jQuery和underscore.js的应用程序.我希望能够在某些jQuery对象上使用下划线的迭代器功能,例如any()all().有没有办法做到这一点?我想做类似以下的事情:

I'm am working with an app that uses both jQuery and underscore.js . I'd like to be able to use some of underscore's iterator functions, such as any() and all() over a collection of jQuery objects. is there a way to do this? I'd like to do something similar to the following:

checkboxes = $("input[type=checkbox]");
_.filter(checkboxes, function(box) {
    return box.is(":checked");
});

但这会引发错误:

Uncaught TypeError: Object #<HTMLInputElement> has no method 'is'

所以我假设在这种情况下box的作用不像jQuery对象.

so I'm assuming box in this context isn't acting like as a jQuery object.

推荐答案

您必须将box包装在jQuery中:

You have to wrap box in jQuery:

checkboxes = $("input[type=checkbox]");
checkboxes = _.filter(checkboxes, function(box) {
    return $(box).is(":checked");
});


此外,您可以只使用本机box.checked:

checkboxes = $("input[type=checkbox]");
checkboxes = _.filter(checkboxes, function(box) {
    return box.checked;
});

附带说明: jQuery有自己的过滤方法:

checkboxes = $("input[type=checkbox]").filter(function() {
    return $(this).is(":checked");
});


此外,在您的示例中-确定要过滤吗?您可以像选择器一样轻松地使用它:


Furthermore, in your example - are you sure you have to filter? You could just as easily use that as your selector:

checkboxes = $("input[type=checkbox]:checked")

这篇关于在jquery对象的集合上使用underscore.js列表函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆