开发者

Add variable to json string or swap on click?

开发者 https://www.devze.com 2023-03-10 22:43 出处:网络
Ok I using something like this $.getJSON(\"http://myurl.com/search-ext.json&type=aaaaaa&callback=?\",

Ok I using something like this

$.getJSON("http://myurl.com/search-ext.json&type=aaaaaa&callback=?", 

working fine but now I need to add another type variable like this:

$.getJSON("http://myurl.com/search-ext.json&type=aaaaaa&type=bbbbb&callback=?",

Yes, they ha开发者_如何学Pythonve the same name.... so can I insert another type or is there a way to swap out the string with an onclick?


Add [] to the key in your URL:

$.getJSON("http://myurl.com/search-ext.json&type[]=aaaaaa&type[]=bbbbbb&callback=?",

Also, I don't know if you did this on purpose or not, but it at least appears as if you need a ? after .json:

$.getJSON("http://myurl.com/search-ext.json?type[]=aaaaaa&type[]=bbbbbb&callback=?",

Then, for instance in PHP, you will have an array on the server:

$_GET['type'] ~ array('aaaaaa','bbbbbb')

EDIT

Following the additional comments, I believe this is what you're looking for:

http://plugins.jquery.com/project/query-object

var newerQuery = $.query.SET('type', 'bbbbb');
var newURL = "http://myurl.com/search-ext.json?" + newerQuery;
$.getJSON(newURL);

An example:

http://jfcoder.com/test/gettypes.php?type=aaaaaa&callback=MyCallback

EDIT 2

<a href="href://www.somewhere.com/?type=aaaaaa&amp;other=yes&amp;another=yes&amp;yeti=neverseen" 
   onClick="return replaceType(this,'bbbbbb')">Link</a>

function replaceType(el, value) {
    sub = el.href.substring(0,el.href.indexOf('?'));
    split = el.href.substring(el.href.indexOf('?')+1).split("&");
    for (var i = 0; i < split.length; i++) {
        if (split[i].indexOf('type=') !== 0) continue;
        split[i] = encodeURI("type="+value);
    }
    el.href = sub + "?" + split.join('&');
    alert(el.href);
    return false;
}

http://jsfiddle.net/Phjsv/1/

0

精彩评论

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