将URL参数传递给Colorbox

时间:2012-10-08 05:14:29

标签: javascript html colorbox

我有一个简单的投票系统,彩盒作为投票表格。当一个人点击投票时,会将他们带到vote.html?id = X(x是一个数字)。 vote.html显示在颜色框中。在颜色框中,我获取了URL参数,但它没有找到id作为参数。知道如何将id传递到colorbox吗?这是代码......

使用Javascript:

<script>
    function voteForShirt(shirtId) {
        alert("vote.htm?="+shirtId);
        $('#').colorbox();
        $.colorbox({href:"vote.html?id="+shirtId});
    }
</script>

以下是出现在colorbox

中的来自vote.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>

在这里,当我提醒shirtId时,我得到了不确定。

2 个答案:

答案 0 :(得分:0)

我不知道你的正则表达式有什么问题,但是尝试使用这个函数来解析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>

来源:Artem Barger

答案 1 :(得分:0)

看起来您应该使用iframe而不是ajax。使用ajax时,您没有创建新的窗口对象,因此window.location指的是您当前的位置(而不是vote.html)。

相关问题