how to get GET and POST variables with JQuery?

2019-01-01 01:57发布

How do I simply get GET and POST values with JQuery?

What I want to do is something like this:

$('#container-1 > ul').tabs().tabs('select', $_GET('selectedTabIndex'));

14条回答
忆尘夕之涩
2楼-- · 2019-01-01 02:03

There's a plugin for jQuery to get GET params called .getUrlParams

For POST the only solution is echoing the POST into a javascript variable using PHP, like Moran suggested.

查看更多
谁念西风独自凉
3楼-- · 2019-01-01 02:04

Use following function:

var splitUrl = function() {
    var vars = [], hash;
    var url = document.URL.split('?')[0];
    var p = document.URL.split('?')[1];
    if(p != undefined){
        p = p.split('&');
        for(var i = 0; i < p.length; i++){
            hash = p[i].split('=');
            vars.push(hash[1]);
            vars[hash[0]] = hash[1];
        }
    }
    vars['url'] = url;
    return vars;
};

and access variables as vars['index'] where 'index' is name of the get variable.

查看更多
看风景的人
4楼-- · 2019-01-01 02:08

You can try Query String Object plugin for jQuery.

查看更多
爱死公子算了
5楼-- · 2019-01-01 02:09

jQuery plugins seem nice but what I needed is a quick js function to parse the get params. Here is what I have found.

http://www.bloggingdeveloper.com/post/JavaScript-QueryString-ParseGet-QueryString-with-Client-Side-JavaScript.aspx

查看更多
高级女魔头
6楼-- · 2019-01-01 02:11

Keep it simple

replace VARIABLE_KEY with the key of the variable to get its value

 var get_value = window.location.href.match(/(?<=VARIABLE_KEY=)(.*?)[^&]+/)[0]; 
查看更多
还给你的自由
7楼-- · 2019-01-01 02:12

why not use good old PHP? for example, let us say we receive a GET parameter 'target':

function getTarget() {
    var targetParam = "<?php  echo $_GET['target'];  ?>";
    //alert(targetParam);
}
查看更多
登录 后发表回答