jQuery and generating links
How can I get jQuery to generate the link for a get such that I don't have to try to hand code all the query parameter开发者_JS百科s?
Alternative to doing this:
$('a.csvBtn').attr('href',"CSVReporter?cam=" + id + "&range);
As we know jquery.get({url:n, data:xxx});
and jQuery takes the data and builds a link to the url http://blah.com/n?x=1&x=2.
You can use jQuery.param
to convert an object into a query string.
var qs = {};
qs.cam = id;
qs.range = 'blah';
var query_string = jQuery.param(qs); //for id=foo, this will be cam=foo&range=blah
Or, in use:
$('a.csvBtn').attr('href', function(){
var qs = {cam : id, range : 'blah'};
return "CSVReporter?"+jQuery.param(qs);
});
精彩评论