2013-10-14 35 views
1

我有以下代碼:一類添加到功能goForit()

function goForit() { 
    var passwd; 
    passwd = document.forms['giftForm'].pass.value; 
    if(passwd=="yes"){ 
    window.open ('eat.html','_self',false) 
    } 
    else{ 
    alert('Password wrong'); 
    } 
} 

<form id="giftForm">   
    <input type="text" id="pass" placeholder="" style="font-size:150%;"/> 
    <br /> 
    <input type="button" onClick="goForit()" value="Submit" style="font-size:100%; margin-top:15px;" />     
</form> 

如果有人輸入「是」作爲密碼,並點擊提交按鈕。用戶將被重定向到eat.html頁面。

我想通過.carrot類與重定向,所以正確的圖像將出現。如何將.carrot類添加到以下代碼?

+1

如果您也使用PHP,那麼您可以使用GET來傳遞它。不知道是否只有javascript纔可能。 –

+2

哦,上帝,請告訴我,這不是你如何認證用戶。 – xbonez

+0

@xbonez甚至沒有提起......我會死的哈哈。 –

回答

2

到這裏看看:

How to redirect on another page and pass parameter in url from table?

HTML:

<input type="button" name="theButton" value="Detail" class="btn" data-username="{{result['username']}}" /> 

然後檢查重定向的URL的位置:

的Javascript:

$(document).on('click', '.btn', (function() { 

    var name = $(this).data('username'); 

    if (name != undefined || name != null) { 
     window.location = '/player_detail?username=' + name; 
    } 
});​ 

此外,另一個StackOverflow的問題:

Javascript redirect and Pass Argument to redirected Page

+1

@tymeJV哈,對不起,讓我用Javascript重寫 –

+1

以爲我會讓你知道之前的一個downvotes冰雹飛來飛去:D – tymeJV

+0

哈哈,upvote爲那個:D –

1

你已經接受了答案,但這裏的那是更接近原始,不需要jQuery的一種替代方案:

<button type="button" value="goForit(this)" value="carrot" ... >Go for it</button> 

然後在功能:

function gotForit(el) { 
    if (el.form.pass.value.toLowerCase() == 'yes') { 
    window.location = 'eat.html?username=' + el.value; 
    } else { 
    alert('oops…'); 
    } 
}