Javascript jQuery 多 ID 选择器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7079011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:45:33  来源:igfitidea点击:

jQuery Multiple ID selectors

javascriptjquery

提问by Webby

Here's a snippet of the start of my code:

这是我的代码开头的片段:

var myUpload = $("#upload_link").upload({bla bla bla

Basically what I'm trying to do is make the same call with a few different ID's...

基本上我想做的是用几个不同的 ID 拨打相同的电话......

I would have assumed this would work but it doesn't:

我会假设这会起作用,但它不会:

var myUpload = $("#upload_link,#upload_link2,#upload_link3").upload({

Any ideas?

有任何想法吗?

回答by rsplak

Try this:

尝试这个:

$("#upload_link,#upload_link2,#upload_link3").each(function(){
    $(this).upload({
        //whateveryouwant
    });
});

回答by tonycoupland

If you give each of these instances a class you can use

如果你给每个这些实例一个类,你可以使用

$('.yourClass').upload()

回答by zzzzBov

You canuse multiple id's the way you wrote:

可以id按照您编写的方式使用 multiple :

$('#upload_link, #upload_link2, #upload_link3')

However, that doesn't mean that those ids exist within the DOM when you've executed your code. It also doesn't mean that uploadis a legitimate function. It also doesn't mean that uploadhas been built in a way that allows for multiple elements in a selection.

但是,这并不意味着在您执行代码时这些 id 存在于 DOM 中。这也不意味着这upload是一个合法的功能。这也不意味着它的upload构建方式允许选择中的多个元素。

uploadis a custom jQuery plugin, so you'll have to show what's going on with uploadfor us to be able to help you.

upload是一个自定义的 jQuery 插件,因此您必须向我们展示正在发生的事情upload才能为您提供帮助。

回答by ShankarSangoli

Make sure uploadplugin implements this.eachin it so that it will execute the logic for all the matching elements. It should ideally work

确保upload插件this.each在其中实现,以便它执行所有匹配元素的逻辑。它应该理想地工作

$("#upload_link,#upload_link2,#upload_link3").upload(function(){ });

回答by Joseph Marikle

it should. Typically that's how you do multiple selectors. Otherwise it may not like you trying to assign the return values of three uploads to the same var.

这应该。通常这就是您使用多个选择器的方式。否则它可能不喜欢您尝试将三个上传的返回值分配给同一个 var。

I would suggest using .eachor maybe push the returns to an array rather than assigning them to that value.

我建议使用.each或可能将返回值推送到数组,而不是将它们分配给该值。

回答by Jeremy Holovacs

That should work, you may need a space after the commas.

这应该可行,您可能需要在逗号后留一个空格。

Also, the function you call afterwards must support an array of objects, and not just a singleton object.

此外,您之后调用的函数必须支持一个对象数组,而不仅仅是一个单例对象。