2012-05-10 48 views
0

我正在工作一個頁面,用戶有兩個按鈕。一個按鈕只是將它們重定向到一個主頁,另一個按鈕不僅將它們重定向到主頁,還會向另一個鏈接打開一個新窗口/選項卡。如何使用JQuery同時重定向並打開一個新窗口?

 <button class="return" type="button"> 
     <?= $html->image('cross.gif') ?> Return 
     </button> 
     <button class="walletcards" type="button"> 
     <?= $html->image('cross.gif') ?> Print Wallet Cards 
     </button> 

    <script type="text/javascript"> 
     $(document).ready(function() { 
     $('button.return').click(function() { 
     $(window.location).attr('href','/accounts/view/<?=$data['Email'] ['acctgrpid']; ?>'); 
}); 

$('button.walletcards').click(function(){ 
$(location).attr('href', '/accounts/view/<?=$data['Email']['acctgrpid']; ?>') 
}); 

$('button.walletcards').click(function(){ 
    window.close(); 
    window.open('/wallet_cards/account/<?=$data['Email']['acctgrpid']; ?>'); 
    }); 

現在我的問題這個工作,但它打開兩個窗口/標籤的鏈接(「/ wallet_cards /帳戶....)我如何阻止它打開兩個窗口/標籤,還是有更好的辦法,爲什麼不試試把這個2點的動作在同一個功能,我可以做到這一點。

+0

...將它們集成到一個:

所以我會更新的東西等等。 – TRR

回答

0

$('button.walletcards').click(function(){ 
    window.close(); 
    window.open('/wallet_cards/account/<?=$data['Email']['acctgrpid']; ?>'); 

$(location).attr('href', '/accounts/view/<?=$data['Email']['acctgrpid']; ?>') 
}); 
+0

我反轉'open'和'close'方法調用的順序,也不會使用jQuery爲'location.href'賦值,因爲這已經可以在'window'上得到了。 – MilkyWayJoe

1

你的代碼是在它是做什麼與你所描述的有點混亂。看起來您的代碼正在做的是將2個點擊事件添加到按鈕,一個重定向到帳戶,o ne關閉窗口並打開一個新的wallet_cards /帳戶。另外你的window.close()出現在你的window.open()之前...我會反轉這些以防止出現問題。爲什麼你對按鈕點擊2個事件處理

$('button.walletcards').click(function() { 
    window.open('/wallet_cards/account/<?=$data['Email']['acctgrpid']; ?>'); 
    window.location.href = '/wallet_cards/account/<?=$data['Email']['acctgrpid']; ?>'; 
    //window.close(); // Not sure why you are redirecting but then closing the window... 
}); 
+0

你可能是指'window.location .href ='/ wallet_cards/account/<?= $ data ['Email'] ['acctgrpid']; '>';' – MilkyWayJoe

+0

是的,謝謝!適當更新代碼。太習慣使用jquery的一切... – tonyellard