简体   繁体   English

jQuery 多个ID选择器

[英]jQuery Multiple ID selectors

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?有任何想法吗?

Try this:尝试这个:

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

If you give each of these instances a class you can use如果你给每个实例一个 class 你可以使用

$('.yourClass').upload()

You can use multiple id 's the way you wrote:可以按照您编写的方式使用多个id

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

However, that doesn't mean that those ids exist within the DOM when you've executed your code.但是,这并不意味着当您执行代码时这些 id 存在于 DOM 中。 It also doesn't mean that upload is a legitimate function.这也不意味着upload是合法的function。 It also doesn't mean that upload has been built in a way that allows for multiple elements in a selection.这也不意味着upload的构建方式允许选择中的多个元素。

upload is a custom jQuery plugin, so you'll have to show what's going on with upload for us to be able to help you. upload是一个自定义的 jQuery 插件,因此您必须展示upload的情况,我们才能为您提供帮助。

Make sure upload plugin implements this.each in it so that it will execute the logic for all the matching elements.确保upload插件在其中实现this.each ,以便它将执行所有匹配元素的逻辑。 It should ideally work理想情况下它应该工作

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

If all your elements starting with upload_ in its id have the same purpose or syntax you could try and use the following:如果您的所有元素在其id中以upload_开头,都具有相同的目的或语法,您可以尝试使用以下内容:

$("*[id^='upload_']").each(function() {
    $(this).upload()
});

This way you don't have to specify every single element in the selector.这样您就不必指定选择器中的每个元素。

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 .each or maybe push the returns to an array rather than assigning them to that value.我建议使用.each或者将返回值推送到数组而不是将它们分配给该值。

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.此外,您之后调用的 function 必须支持对象数组,而不仅仅是 singleton object。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM