1

I have following code:

var elem1 = $("#elem1");
var elem2 = $("#elem2");
var elem3 = $("#elem3");

I want to disable all these in single line of jquery code using the var names. I know jquery allows to find the multiple elements using single $("#elem1, #elem2") but I want to use the variable name and disable them at once, something like:

elem1.elem2.elem3.prop("disabled", true);

Is it possible? How to do this?

4

2 回答 2

6

由于您已经选择了对象,因此可以使用add()

var elem1 = $("#elem1");
var elem2 = $("#elem2");
var elem3 = $("#elem3");

elem1.add(elem2).add(elem3).prop("disabled", true);

如果您没有选择对象,最好使用类选择器。

于 2013-09-16T12:21:58.823 回答
3

您可以使用attributes-starts-with选择器

$('[id^="elem"]').prop('disabled',true);
于 2013-09-16T12:17:47.747 回答