2014-03-25 53 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