将参数传递给 jQuery 'Load'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6410457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Passing Parameters to a jQuery 'Load'
提问by JasonMHirst
I have some script on my page which is:
我的页面上有一些脚本,它是:
$('#divMenuHolder').load('../menu.html');
However I also need to be able to pass some parameters which are acted on the menu.html page.
但是,我还需要能够传递一些在 menu.html 页面上起作用的参数。
I've tried the following:
我尝试了以下方法:
$('#divMenuHolder').load('../menu.html?opt1=test&opt2=test2');
However when I come to get the parameters using the following function:
但是,当我使用以下函数获取参数时:
function getUrlVars()
{
var vars = [], hash;
var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}
It returns nothing because it's trying to get the parameters of the page itself, and not the page I'm loading into DIV.
它什么都不返回,因为它试图获取页面本身的参数,而不是我加载到 DIV 中的页面。
Can anyway assist in guiding me to what I want please?
无论如何都可以帮助指导我做我想要的吗?
回答by Prasad Chavan
This works for me
这对我有用
$('#semester').load("restricted/getSemester.php?courseid=" + $("#course").val());
where #course
is id of input field.
#course
输入字段的 id在哪里。
OR
或者
$('#subject').load("restricted/getSubject.php?courseid=0&semester=0");
I hope this helps u :)
我希望这对你有帮助:)
回答by Kon
According to this jQuery load() documentation example:
Example: pass arrays of data to the server.
$("#objectID").load("test.php", { 'choices[]': ["Jon", "Susan"] } );
示例:将数据数组传递给服务器。
$("#objectID").load("test.php", { 'choices[]': ["Jon", "Susan"] } );
Function definition:
函数定义:
.load( url, [data,] [complete(responseText, textStatus, XMLHttpRequest)] )
url
A string containing the URL to which the request is sent.
data
A map or string that is sent to the server with the request.
complete(responseText, textStatus, XMLHttpRequest)
A callback function that is executed when the request completes.
.load( url, [data,] [complete(responseText, textStatus, XMLHttpRequest)] )
url
包含请求发送到的 URL 的字符串。
data
随请求发送到服务器的映射或字符串。
complete(responseText, textStatus, XMLHttpRequest)
请求完成时执行的回调函数。
回答by srini
I ran in to a similar problem. Was Just wondering if you have found the solution. I found a workaround but its not suitable for my application.
我遇到了类似的问题。只是想知道您是否找到了解决方案。我找到了一种解决方法,但它不适合我的应用程序。
One of the options is to set the cookie before you load the URl and access the cookie in the seperate html page. But cookies can be disabled so you have to be careful on that part.Also if your separate html is going to reusable , then you need to figure out the perfect solution , cookies are not useful in that case.
选项之一是在加载 URl 并在单独的 html 页面中访问 cookie 之前设置 cookie。但是 cookie 可以被禁用,所以你在这方面必须小心。另外,如果你的单独的 html 是可重用的,那么你需要找出完美的解决方案,在这种情况下 cookie 没有用。
回答by genesis
Try following
尝试以下
var url_to_load = '../menu.html?opt1=test&opt2=test2';
$('#divMenuHolder').load(url_to_load);
function getUrlVars(url_to_load)
{
var vars = [], hash;
var hashes = url_to_load.slice(url_to_load.indexOf('?') + 1).split('&');
for(var i = 0; i < hashes.length; i++)
{
hash = hashes[i].split('=');
vars.push(hash[0]);
vars[hash[0]] = hash[1];
}
return vars;
}