How to add the ID in jquery without using Short Hand Operator

时间:2019-01-09 22:19:44

标签: jquery

How to add the ID in jquery without ShortHand Operator

I want to close Bootstrap alert boxes, so I use this line

var newAlertIds=0;
$("#"+ newAlertIds).alert('close');

But sometimes I need to close the alert box whose value is greater by one digit. Like this

$("#"+ newAlertIds+1).alert('close');

But the above line is not working. Any suggestions?

Remember I dont want to use Short Hand Operators like this

 $("#"+ newAlertIds++).alert('close');

1 个答案:

答案 0 :(得分:0)

This is related on how (the implicit order) the interpreter executes the expressions. In your case:

"#" + newAlertIds + 1

will be evaluated to

("#" + newAlertIds) + 1

So, assuming newAlertIds is an integer, ("#" + newAlertIds) executes first generating a string result and then 1 is concatenated to that string. For example, if newAlertIds is equal to 24, you will get the result of #241.

So you will need to force (some way) the implicit order for evaluating your expression, like this:

"#" + (newAlertIds + 1)

This will output #25 in relation to previous example.