我如何使用JQuery同时重定向和打开一个新窗口?

时间:2012-05-10 14:38:09

标签: jquery redirect hyperlink window

我正在使用一个用户有两个按钮的页面。一个按钮只是将它们重定向到主页,另一个按钮不仅将它们重定向到主页,而且还打开一个新的窗口/标签到另一个链接。

     <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 / account ....”)如何阻止它打开两个窗口/标签,或者有​​更好的方法我可以这样做。

2 个答案:

答案 0 :(得分:1)

您的代码与您所描述的内容相比有点令人困惑。看来你的代码正在做的是向按钮添加2个点击事件,一个重定向到帐户,一个关闭窗口并向wallet_cards / account开一个新的。

另外你的window.close()出现在你的window.open()之前...我会反转那些以防止那里的问题。

所以我会更新为:

$('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...
});

答案 1 :(得分:0)

为什么不尝试将这2个动作放在同一个函数中?

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

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