輸出cookie的值到提示框 - outputing value of cookies to prompt box -开发者知识库

輸出cookie的值到提示框 - outputing value of cookies to prompt box -开发者知识库,第1张

this code doesnt properly display the cookie value, it has the name of the cookie before for example email=.... I just want the value to appear? the cookies are saving fine as i can view them in google chrome settings

這段代碼沒有正確顯示cookie值,它之前有cookie的名稱,例如email = ....我只想要顯示值? Cookie正在保存,因為我可以在谷歌瀏覽器設置中查看它們

<script type="text/javascript">


function setUserCookie() {
var els = document.forms.frm1.elements;
var cookiesToSet = ['first_name','email','fav'];
for (var i=cookiesToSet.length;i--;){
  var name = cookiesToSet[i];
  setCookie(name, els[name].value, 365);
  prompt(document.cookie);
}

}

    <form id="frm1">
            <input name="first_name" required="true" type="text" id="first_name"> 
            <input name="email" required="true" type="text" id="email">

            <h4>Favourite Destination</h4><select required="" id="fav">
                <option>
                    Copenhagen
                </option>

                <option>
                    Helsinki
                </option>

                <option>
                    Oslo
                </option>

                <option>
                    Stockholm
                </option>
            </select> <input id="SideBarSubmit" type="submit" value=
            "Subscribe"  onclick="setUserCookie();" >
        </form>

1 个解决方案

#1


As your code is shown, when you store the value to your cookie, you used els[name] instead of els[name].value. You can either change your value store to els[name].value:

當您的代碼顯示時,將值存儲到Cookie時,您使用els [name]而不是els [name] .value。您可以將值存儲更改為els [name] .value:

els[name].value = "abcd";

or retrieve value on els[name]:

或檢索els [name]上的值:

function setUserCookie() {
    var els = document.forms.frm1.elements;
    var cookiesToSet = ['first_name','email','fav'];
    for (var i=cookiesToSet.length;i--;){
        var name = cookiesToSet[i];
        setCookie(name, els[name], 365);
        prompt(document.cookie);
    }
}

最佳答案:

本文经用户投稿或网站收集转载,如有侵权请联系本站。

发表评论

0条回复