Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/462.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Jquery中从URL获取参数值?_Javascript_Jquery - Fatal编程技术网

Javascript 如何在Jquery中从URL获取参数值?

Javascript 如何在Jquery中从URL获取参数值?,javascript,jquery,Javascript,Jquery,大家好,我有一个url,我需要从url获取一个参数 var URL="http://localhost:17775/Students/199/Kishore" //here from the url i need to get the value 199 这是我一直在尝试的,但是这里的值是空的 function getURLParameter(name) { return parent.decodeURI((parent.RegExp(name + /([^\/]+)

大家好,我有一个url,我需要从url获取一个参数

   var URL="http://localhost:17775/Students/199/Kishore"
   //here from the url i need to get the value 199
这是我一直在尝试的,但是这里的值是空的

  function getURLParameter(name) { 
    return parent.decodeURI((parent.RegExp(name + /([^\/]+)(?=\.\w+$)/).exec(parent.location.href) || [, null])[1]); 
  };

  $(document).ready(function() {
     getURLParameter("Students");
     //i need to get the value 199 from the url
  });

尽管可以使用jQuery,但它并不需要。有很多方法可以剥这只猫的皮。像这样的事情应该会让你朝着正确的方向开始:

var URL="http://localhost:17775/Students/199/Kishore";
var splitURL = URL.split("/");
var studentValue = "";

for(var i = 0; i < splitURL.length; i++) {
    if(splitURL[i] == "Students") {
        studentValue = splitURL[i + 1];
        break;
    }
}

如果你想要的块总是在同一个地方,这将起作用

var url="http://localhost:17775/Students/199/Kishore"

//break url into parts with regexp
//removed pointless regexp
var url_parts = url.split('/'); 

//access the desired chunk
var yourChunk = url_parts[4]

console.log(yourChunk)

我会这样做:

var url = "http://localhost:17775/Students/199/Kishore"; 
var studentValue = url.match('/Students/(\\d+)/')[1]; //199

这就是您要查找的内容,因为URL参数将不断更改:


功能
?请更正
函数
名称+
为什么要将字符串连接到RegExp?正如您所说,值不能每次都是学生,因此我无法将其等同于学生said@user2152019,#1,提出问题后不要改变要求,#2,我认为你不明白decodeURI的作用。查一下,我没有改变任何要求,我只是说我们不能完全依赖参数passed@user2152019嗯,那我就完全糊涂了。我们不是在这里建立人工智能。要编写从字符串中提取数据的方法,我们需要“提取规则”。它们必须是不变的。那么,规则是什么?职位会是一样的吗?目录(即学生)是否相同?什么是相同的?@user215219以及您将“ID的位置将是相同的”定义为什么?当
.split()
足够时,为什么要用正则表达式将事情复杂化?您的正则表达式在此上下文中不起任何作用
.split()
正在做所有的工作。所以你问了这个问题,否决了我对regexp的回答,这是毫无意义的,然后像我建议的那样使用.split('/')。从我的回复中删除否决票怎么样?在ie8、chrome和firefox上进行了测试。它们都返回了199。只有一件事,
“\/(.*)\/”
应该是
“\\/(.*)\/”
,不是吗?
var url = "http://localhost:17775/Students/199/Kishore"; 
var studentValue = url.match('/Students/(\\d+)/')[1]; //199
var URL="http://localhost:17775/Students/199/Kishore"
var number = getNumber('Students'); //199

var URL="http://localhost:17775/Teachers/234/Kumar"
var number = getNumber('Teachers'); //234

function getNumber(section) {
  var re = new RegExp(section + "\/(.*)\/","gi");
  var match = re.exec(URL);
  return match[1];
}