简体   繁体   English

如何获取url参数值并使用jQuery将其传递给url

[英]How to get url params value and pass it in url using jQuery

I having url in query string like http://www.domain.com:3000/allot_room?floor=6&hostel=1&student=1.In that, how to get student parameter value using jquery. 我在查询字符串中有url,例如http://www.domain.com:3000/allot_room?floor=6&hostel=1&student=1。那样,如何使用jquery获取学生参数值。

I have used this code 我已经使用了这段代码

var url = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

It shows like floor=6,hostel=1,student=1.In that how to get student parameter value.And I tried different codes. 它显示为floor = 6,hostel = 1,student = 1。那样如何获取学生参数值。我尝试了不同的代码。

use var url = window.location.search 使用var url = window.location.search

and from MDN - MDN-

Get the value of a single window.location.search key: 获取单个window.location.search键的值:

function loadPageVar (sVar) {
  return decodeURI(window.location.search.replace(new RegExp("^(?:.*[&\\?]" + encodeURI(sVar).replace(/[\.\+\*]/g, "\\$&") + "(?:\\=([^&]*))?)?.*$", "i"), "$1"));
}

alert(loadPageVar("floor"));
alert(loadPageVar("student"));

More about window.location 有关window.location的更多信息

try 尝试

window.location.search

see mozilla documentation 请参阅mozilla文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM