Why this jquery selector gives me error? uncaught exception: Syntax error, unrecognized expression: ''
I am using this code ti dynamically select attributes of a element but it gives me an error in firebug
Error:
uncaught exception: Syntax error, unrecognized expression: ''
Here is my code:
jQuery('.mydata').click(function(){
var current_id=jQuery(this).attr('id');
var current_datatype=jQuery(this).attr('datatype');
var next_id=parseInt(current_id);
next_id=next_id+1;
next_id="'#"+next_id+"'";
var next_datatype=jQuery(next_id).attr('datat开发者_高级运维ype'); //this line gives error
});
What if you do
next_id = "#" + next_id;
instead of next_id="'#"+next_id+"'";
when selecting an id you don't need the quotes if you assign it to a variable
change:
next_id="'#"+next_id+"'";
var next_datatype=jQuery(next_id).attr('datatype'); //this line gives error
into:
next_id="#"+next_id;
var next_datatype=jQuery(next_id).attr('datatype'); //this line gives error
I think you don't need the extra quotes here
next_id="'#"+next_id+"'";
should be read
next_id="#"+next_id;
change this ...
next_id="'#"+next_id+"'";
to this ...
next_id="#"+next_id;
Your selector becomes something like '#2'
instead of #2
. You need to remove the extra '. Also you should never use parseInt without passing in a radix, like parseInt(currentId, 10)
.
精彩评论