[英]Passing a URL Parameter to a Colorbox
I have a simple voting system with a colorbox as the voting form. 我有一个简单的投票系统,带有一个颜色框作为投票表。 When a person clicks on vote, it takes them to vote.html?id=X (x being a number). 当某人点击投票时,便需要他们投票。html?id = X(x是数字)。 vote.html gets displayed in the colorbox. 投票.html将显示在颜色框中。 In the colorbox, I get the URL Parameters, but it does not find id as a parameter. 在颜色框中,我获得了URL参数,但没有找到id作为参数。 Any idea how to pass id into the colorbox? 任何想法如何将id传递到colorbox? Here's the code... 这是代码...
Javascript: Javascript:
<script>
function voteForShirt(shirtId) {
alert("vote.htm?="+shirtId);
$('#').colorbox();
$.colorbox({href:"vote.html?id="+shirtId});
}
</script>
The following is Javascript from vote.html that appears in the colorbox 以下是出现在颜色框中的来自表决.html的Javascript
<script type="text/javascript">
function getUrlVars() {
var vars = {};
var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
vars[key] = value;
});
return vars;
}
var shirtId = getUrlVars()["id"];
alert(shirtId);
document.getElementById('title').innerHTML = "<h1>You're Voting for Shirt " + shirtId + "</h1>";
</script>
Here when I alert shirtId, I get undefined. 在这里,当我警告shirtId时,我将无法定义。
I don't know what's wrong with your regexp, but try using this function to parse the id instead. 我不知道您的regexp有什么问题,但是请尝试使用此函数来解析ID。
<script type="text/javascript">
function getParameterByName(name)
{
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.search);
if(results == null)
return "";
else
return decodeURIComponent(results[1].replace(/\+/g, " "));
}
</script>
Source: Artem Barger 资料来源: Artem Barger
It looks like you should be using an iframe instead of ajax. 它看起来像你应该使用iframe,而不是AJAX的。 You aren't creating a new window object when you use ajax, so window.location refers to your current location (not vote.html). 使用ajax时不会创建新的窗口对象,因此window.location指的是当前位置(而不是vote.html)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.