开发者

Quick question. I need to add a variable to my javascript object

开发者 https://www.devze.com 2023-04-05 12:24 出处:网络
So I have a question. It\'s probably simple, but I\'ve tried so many things and can\'t seem to get it to work. If you can just quickly rewrite this in the correct syntax, that would be sooo awesome.

So I have a question. It's probably simple, but I've tried so many things and can't seem to get it to work. If you can just quickly rewrite this in the correct syntax, that would be sooo awesome.

I need my javascript object to include a variable that's a counter that adds.


Here is my code that works.

$(div_1).droppable("option", "disabled", false);

Here is what i need to work.

var counter = 1;

$('div_' + counter).droppable("option", "disabled", false);

counter++;

开发者_Python百科Hopefully i explained this well enough...

Thank you so much!!


Interesting, this is how I would do it:

var counter=1;
$(window['div_' + counter]).droppable("option", "disabled", false);
counter++;

This assumes div_1 is a variable, as your question is written. By getting the element out of the window array, I avoid the nasty nasty eval.


If div_x is the name of the element id, then:

var counter = 1;

$("#div_" + counter).droppable("option", "disabled", false);

counter++;

The # matches element id's, "." matches element classes. Simply "div" would match all divs on the page.


Your code should work. if you add a # before your string and you're targeting $('#div_1) (a div with id="div_1") Basiclly you can add a Number type variable to an string and result is string

var n = 1; // n is Number type
var hey = "hey "; // hey is an String type

console.log(hey + n); // returns "hey 1"
console.log(n + hey); // returns "1hey "

What I'm guessing is you declared the div_1 before like div_1 = $('#div1');. So if you pass "div_1" to your jQuery selector you will fail

0

精彩评论

暂无评论...
验证码 换一张
取 消