阅读及;在javascript中使用cookie值

阅读及;在javascript中使用cookie值,javascript,cookies,Javascript,Cookies,刚开始,我对javascript非常陌生,对任何愚蠢的评论或明显的错误表示道歉 这就是我目前正在处理的问题: <div id="currency_select"> <form action="/Default.asp?" method="post" name="CurrencyChoice"> <select onchange="this.form.submit()" name="ER_ID"> <opti

刚开始,我对javascript非常陌生,对任何愚蠢的评论或明显的错误表示道歉

这就是我目前正在处理的问题:

<div id="currency_select">
    <form action="/Default.asp?" method="post" name="CurrencyChoice">
        <select onchange="this.form.submit()" name="ER_ID">
            <option value="3">EUR</option>
            <option value="2">GBP</option>
        </select>
        <script type="text/javascript">
            document.forms['CurrencyChoice'].elements['ER_ID'].value = '';
        </script>
    </form>
</div>
但是,我仍然不确定如何在值字段中返回适当的结果


提前感谢您的帮助。

很抱歉,我不想重写这篇文章,它非常全面,甚至为您提供了完整的读写cookie功能

对于一般的JS学习来说,它也是一个很好的资源,所以你一定要看看它

代码如下:

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 "";
}
但你可以在自己的网站上阅读更多

编辑:以下是在您的具体案例中承诺的全功能示例:

<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <title>Cookie Example</title>
        <script type="text/javascript">

        //adds the [String].trim() method to the [String] object if missing
        if(!String.prototype.trim) {
          String.prototype.trim = function () {
            return this.replace(/^\s+|\s+$/g,'');
          };
        }

            var cookieName = "ER_ID";

            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 setOptionFromCookie() {

                var select = document.getElementById('ER_ID'), index, value = getCookie(cookieName);

                index = select.options.length;
                while(index--) {
                    if(select.options[index].value == value) {
                        break;
                    }
                }

                if(index > -1) {
                    select.selectedIndex = index;
                } else {
                    alert('no such value in selection');
                }
            }

            function setValue() {
                var value = document.getElementById('value').value;
                setCookie(cookieName, value, 365);
            }
        </script>
    </head>
    <body>
        The value that will go into the cookie "ER_ID": <input id="value" value="2" />
        <br/>
        <button onclick="setValue()">Set Cookie</button>
        <button onclick="setOptionFromCookie()">Set Option from cookie</button>
        <hr />
        <div id="currency_select">
            <form action="/Default.asp?" method="post" name="CurrencyChoice">
                <select onchange="this.form.submit()" id="ER_ID" name="ER_ID">
                    <option value="1" selected="selected">Select Currency</option>
                    <option value="3">EUR</option>
                    <option value="2">GBP</option>
                </select>
                <script type="text/javascript">

                </script>
            </form>
        </div>
    </body>
</html>

Cookie示例
//如果缺少[String].trim()方法,则将其添加到[String]对象
if(!String.prototype.trim){
String.prototype.trim=函数(){
返回此。替换(/^\s+|\s+$/g,');
};
}
var cookieName=“ER_ID”;
函数setCookie(cname、cvalue、exdays)
{
var d=新日期();
d、 设置时间(d.getTime()+(exdays*24*60*60*1000));
var expires=“expires=“+d.togmString();
document.cookie=cname+“=”+cvalue+”;“+expires;
} 
函数getCookie(cname)
{
变量名称=cname+“=”;
var ca=document.cookie.split(“;”);
对于(变量i=0;i-1){
select.selectedIndex=索引;
}否则{
警报(“选择中没有此类值”);
}
}
函数setValue(){
var value=document.getElementById('value').value;
setCookie(cookieName,value,365);
}
将进入cookie“ER_ID”的值:

设置Cookie 从cookie设置选项
选择货币 欧元 英镑

因为我的浏览器中没有这样的cookie,所以我还为您设置了cookie。但它应该相当容易理解。只需先设置cookie,然后按“set Option from cookie”读取cookie并根据值设置选项。

或者如果您不关心旧IE,只需ER_ID.value=''…谷歌“setCookie”,然后根据需要选择“getCookie”如果您关心站点性能……EryID.Value= GoCuffy('Er%5FID),您可能需要考虑使用LoalStalk进行此操作;虽然我不确定cookie是否可以有这样一个url转义名称…我很确定我没有要求你为我写任何东西,而且我在W3School花了三天时间学习各种JS相关的基础知识,包括这一个。不过,谢谢你指出了显而易见的问题。抱歉,如果我的知识不足是一种烦恼…@Zen You误解了,对于阅读和编写cookies,我会给出与W3Scools相同的答案(没有必要重写存在的东西,我自己使用相同的答案),但是如果你仍然不知道如何使用它们,那么我会为你重写我的答案,针对您的具体情况。@这里承诺的ZenAz是一个功能完整的示例,使用您的HTML(大部分)使用与W3中相同的功能,顺便说一句,下次您提问时请注意您所知道的!我再次为我对此事缺乏了解而道歉。我应该首先说明这一点。我真不敢相信我竟然错过了这么简单的“价值id”…虽然我真的不想给你或任何人写这篇文章带来不便,但我非常感激。我觉得比以前更傻了。再次感谢
function checkCookie()
{
var username=getCookie("username");
if (username!="")
  {
  alert("Welcome again " + username);
  }
else
  {
  username = prompt("Please enter your name:","");
  if (username!="" && username!=null)
    {
    setCookie("username",username,365);
    }
  }
} 
<html>
    <head>
        <meta http-equiv="X-UA-Compatible" content="IE=9" />
        <title>Cookie Example</title>
        <script type="text/javascript">

        //adds the [String].trim() method to the [String] object if missing
        if(!String.prototype.trim) {
          String.prototype.trim = function () {
            return this.replace(/^\s+|\s+$/g,'');
          };
        }

            var cookieName = "ER_ID";

            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 setOptionFromCookie() {

                var select = document.getElementById('ER_ID'), index, value = getCookie(cookieName);

                index = select.options.length;
                while(index--) {
                    if(select.options[index].value == value) {
                        break;
                    }
                }

                if(index > -1) {
                    select.selectedIndex = index;
                } else {
                    alert('no such value in selection');
                }
            }

            function setValue() {
                var value = document.getElementById('value').value;
                setCookie(cookieName, value, 365);
            }
        </script>
    </head>
    <body>
        The value that will go into the cookie "ER_ID": <input id="value" value="2" />
        <br/>
        <button onclick="setValue()">Set Cookie</button>
        <button onclick="setOptionFromCookie()">Set Option from cookie</button>
        <hr />
        <div id="currency_select">
            <form action="/Default.asp?" method="post" name="CurrencyChoice">
                <select onchange="this.form.submit()" id="ER_ID" name="ER_ID">
                    <option value="1" selected="selected">Select Currency</option>
                    <option value="3">EUR</option>
                    <option value="2">GBP</option>
                </select>
                <script type="text/javascript">

                </script>
            </form>
        </div>
    </body>
</html>