2012-10-08 91 views
0

我有一個簡單的投票系統,其中以colorbox作爲投票表單。當一個人點擊投票時,它會將他們投票給vote.html?id = X(x是一個數字)。 vote.html顯示在colorbox中。在colorbox中,我得到了URL參數,但它沒有找到id作爲參數。任何想法如何將id傳入colorbox?下面的代碼...將URL參數傳遞給Colorbox

的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,我得到了一個未定義

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

下面是javascript中vote.html。

回答

0

它看起來像你應該使用iframe,而不是AJAX的。當你使用ajax時,你並沒有創建一個新的窗口對象,所以window.location引用了你當前的位置(不是vote.html)。

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

+0

這不是URL正則表達式的問題,我認爲它仍然得到瀏覽器的window.location而不是colorbox的 – Vikram