开发者

jQuery can't get a good formatted Id

开发者 https://www.devze.com 2023-01-09 05:07 出处:网络
I have a problem with this fonction: function test(value){ var id = \"\'\" + value + \"\'\"; $(id).remove()开发者_高级运维;

I have a problem with this fonction:

function test(value){
var id = "'" + value + "'";
$(id).remove()开发者_高级运维;
}

It gets an "Id", add simple quotes and then call the remove function.

The "value" is a generated id by php. For example, I can have:

$var = "$the_id";

When the "test" function is triggered by a click, I get te following error:

Uncaught Syntax error, unrecognized expression: ''

I thought it's because the function cannot get the id. But when I insert an alert in the function ( alert (id) ), it returns the right "id" with the good format ( '#the_id').

How can solve this problem?

Thank you,

regards.


Assuming the value argument to your function is the id then you want to change your function to:

function test(value){
  var id = "#" + value;
  $(id).remove();
}


If the value is already a string of "#the_id" then you don't need to quote it.

0

精彩评论

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