2009-05-05 69 views
2

對於任何具有特定CSS類的鏈接,我想要控制鏈接是打開在同一個窗口中,一個新窗口還是一個彈出窗口(使用onclick)在用戶從一組單選按鈕中選擇 - 然後將該選項保存在cookie中(全部使用jQuery)。任何人都知道如何做到這一點?單選按鈕控件和jQuery的控件鏈接首選項

回答

3

這可能是我怎麼會做...(你將需要jQuery cookie plugin):

<script language="javascript"> 
$(function() { 
    if($.cookie('link_pref')) { 
    var link_pref = $.cookie('link_pref'); 
     $('#link_options_form :radio[value="'+ link_pref+'"]') 
     .attr('checked','checked'); 
    } 
    $.cookie('link_pref',$('#link_options_form :radio:checked').val(), {expires: 0}); 
    $('#link_options_form :radio').unbind('click').bind('click',function() { 
     $.cookie('link_pref', $(this).val(), {expires: 0}); 
    }); 
    $('a').unbind('click').bind('click',function() { 
     var link_pref = $.cookie('link_pref'); 
     var href = $(this).attr('href'); 
     var link_txt = $(this).text(); 
     switch(link_pref) { 
      case 'new': 
       $(this).attr('target','_blank'); 
       return true; 
      case 'pop': 
       window.open(href,link_txt,'width=640,height=480,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbars=yes,copyhistory=yes,resizable=yes'); 
       return false; 
      case 'greybox': 
       // Other options found here: 
       // http://orangoo.com/labs/greybox/advance_usage.html 
       GB_show(link_txt, href); 
       return false; 
      default: 
       $(this).attr('target','_self'); 
       return true; 
     } 
    }); 
}); 
</script> 

<form id="link_options_form"> 
    <label><input type="radio" name="link_options" value="same" /> Open in Same Window</label> 
    <label><input type="radio" name="link_options" value="new" /> Open in New Window</label> 
    <label><input type="radio" name="link_options" value="pop" /> Open in Pop-Up Window</label> 
    <label><input type="radio" name="link_options" value="greybox" /> Open in Greybox</label> 
</form> 

編輯:對不起,我沒有先進行測試。我在那裏有幾個拼寫錯誤,我忘了設置cookie開始(抱歉)。我測試過了,它現在可以和你的HTML一起工作。使用上面新編輯的代碼。 ;-)

編輯2:我添加了一個直接鏈接到cookie插件,以防萬一你出於某種原因沒有使用正確的。

編輯3:個人而言,我不會將單選按鈕設置爲在javascript中檢查...您可以使用您的服務器端語言訪問相同的cookie,我相信。但是,我提供了一種應該在我新編輯的代碼中工作的方式。

編輯4:已檢查的單選按鈕錯誤的初始設置已修復。這次真的應該真的有用。真的。 o_0

+0

感謝您的回覆,凱爾,但我無法讓它工作。以下是我使用的完整頁面代碼:http://textsnip.com/734b04 - 感謝您的幫助。 – user55655 2009-05-05 18:17:37