2014-03-25 58 views
0

我有一個非常簡單的選擇選項,我希望它在點擊時觸發刷新和特定操作。但在開始之前,我想知道如何在任何刷新或操作後保持它的選中狀態。使選擇選項保持選中狀態。餅乾?

<select name="countries" id="countries" style="width:180px;"> 
<option value='us'>United States</option> 
<option value='gb'>United Kingdom</option> 
</select> 

任何想法,我看了看餅乾,但將不勝感激一些幫助。

回答

0

你必須包括jquery.cookies.js

http://code.google.com/p/cookies/wiki/Documentation

<script type="text/javascript"> 
    function change_action(selected_value) 
    { 
     $.removeCookie("test"); 
     $.cookie("test", selected_value); 
     //make select box selected by placing a id 

     //Even this line is not necessary now 
     //$('#'+selected_value).attr('selected', true); 

     //Provide the URL needs to be redirected 
     window.location.href = ''; 
    } 
    </script> 

以下是HTML代碼,

//In php check whether cookie is working 
    <?php 
    echo $_cookie('test'); 
    ?> 
    <select name="countries" id="countries" style="width:180px;" onchange="return change_action(this.value);"> 
     <option value='us' id="us" <?php if($_cookie('test') == 'us') { ?> ?>selected='selected'<?php } ?>>United States</option> 
     <option value='gb' id="gb" <?php if($_cookie('test') == 'gb') { ?> ?>selected='selected'<?php } ?>>United Kingdom</option> 
     </select> 
+0

我認爲我所有這些cookie選項我嘗試過的主要問題是,我沒有正確包含代碼,我已經在這個鏈接上上傳了一段代碼 - http:// jermainecraig .co.uk/country/...這是否設置正確,因爲它仍然不適合我。 – jermainecraig

+0

@ user3272400檢查我編輯的答案。希望你能理解。 –

+0

@ user3272400並通過在JavaScript內部發出警報進行檢查。 –

0

檢查這,jQuery的完成,有一些代碼其中U可能不需要,這是針對剛剛修改的其他一些問題完成的,您可以選擇

$(document).ready(function(e){ 
    var name = "Country="; 
    var ca = document.cookie.split(';'); 
    for(var i=0; i<ca.length; i++) 
    { 
    var c = ca[i].trim(); 
    if (c.indexOf(name)==0) $('#countries').val(c.substring(name.length,c.length)); 
    } 
}); 

$('#countries').change(function(e) 
{ 
    var cookieVal = "Country="+$(this).val(); 
document.cookie = cookieVal ; 

}); 

http://jsfiddle.net/AmarnathRShenoy/HM3Zj/2/

+0

所以不應該在這個鏈接上的選擇器工作? http://jermainecraig.co.uk/country-two/ – jermainecraig

+0

我did not諒解。 –

0
<select name="countries" id="countries" style="width:180px;" onchange="change_language(this.value)"> 
<option value='us' id='us'>United States</option> 
<option value='gb' id='gb'>United Kingdom</option> 
</select> 

<script> 
function setCookie(cname,cvalue,exdays) 
{ 
var d = new Date(); 
d.setTime(d.getTime()+(exdays*24*60*60*1000)); 
var expires = "expires="+d.toGMTString(); 
document.cookie = cname+"="+cvalue+"; "+expires; 
} 




function getCookie(cname) 
{ 
var name = cname + "="; 
var ca = document.cookie.split(';'); 
for(var i=0; i<ca.length; i++) 
    { 
    var c = ca[i].trim(); 
    if (c.indexOf(name)==0) return c.substring(name.length,c.length); 
    } 
return ""; 
} 


function change_language(lang) 
{ 
setCookie("flag_option_dropdown",current_flag,30); 
var flag_current_option_dropdown=getCookie("flag_option_dropdown"); 
document.getElementById(flag_current_option_dropdown).setAttribute("selected","selected"); 
} 

change_language("gb"); //by default 




<script> 

你可能工作語言翻譯,我已經以這種方式解決了這個問題。祝你好運

+0

JS代碼中存在拼寫錯誤:'lang'被傳入'change_language',儘管該函數使用'current_flag' ... – AntonK