【问题标题】:Cookies created with Jquery via radio buttons not transferring通过单选按钮使用 Jquery 创建的 Cookie 不传输
【发布时间】:2014-12-14 22:05:31
【问题描述】:

很简单。我有 3 个单选按钮,我希望人们选择一个单选按钮,单击按钮保存选择,然后转到下一页并使用 cookie。

目前有一个文本输入框和 3 个单选按钮。文本输入正常,收音机不行。

我的想法是,无论您选择哪种收音机,它们的名称都相同,只是设置了不同的值。因此分配给它们的值被保存为cookie,并在下一页检索。不工作。

抱歉忘记了 JSfiddle - http://jsfiddle.net/3kfz7jdf/3/

html -

 <form action="javascript:void(0)">
    <input type="text" name="cookie" id="cookie" value="">
   <div id="pics">



   </div>
   <div id="radio">
   <input type="radio" name="char" id="char1" value="1">
   <input type="radio" name="char" id="char2" value="2">
   <input type="radio" name="char" id="char3" value="3">
   </div>
 <input type="button" name="Fight" id="saveCookie" value="Fight" onclick="window.location.href='Battle.html'">
 </form>

JS -

$(function(){
    // attach event listener to save cookie button
    $('#saveCookie').click(function(){
        // set cookie expiration to 1 year from today
        var expDate = new Date();
        expDate.setFullYear(expDate.getFullYear() + 1);
        // create cookie string
        var cookieStr = $('#cookie').val() + ";expires=" + expDate.toGMTString();
        // create cookie
        var cookieChar = $('#choice').val() + ";expires=" + expDate.toGMTString();
        document.cookie = cookieStr;
        document.choice = cookieChar;
    });

});

我不知道您是否需要检索页面,但我也将其发布。

js-

$(function(){
        // display cookie 
        $('#cookieDisplay').html(document.cookie);
        $('#cookieDisplay').html(document.choice);
    });

提前谢谢。

【问题讨论】:

    标签: javascript jquery html cookies


    【解决方案1】:

    在代码中JQuery.onclick被安排在window.location.href='Battle.html'之后运行。

    要解决这个问题,请将浏览器的重定位放在 cookie 操作的末尾。

    更新:
    真的很抱歉,我错过了代码中的一个主要问题: document.cookie 代表 Cookie 操作,而 document.choice 只是一个定义的变量。 虽然浏览器使用 Cookie 作为域间变量,但其他变量在每个文档页面中都可用。
    希望对你有帮助。

    $(function(){
        // attach event listener to save cookie button
        $('#saveCookie').click(function(){
            // set cookie expiration to 1 year from today
            var expDate = new Date();
            expDate.setFullYear(expDate.getFullYear() + 1);
            // create cookie string
            var cookieStr = $('#cookie').val() + ";expires=" + expDate.toGMTString();
            // create cookie
            var cookieChar = $('#choice').val() + ";expires=" + expDate.toGMTString();
    
            document.cookie = "cookieStr=" + cookieStr;
            document.cookie = "cookieChar=" + cookieChar;
            // now we relocate:
            window.location.href='Battle.html';
        });
    });
    

    要读取每个参数的 cookie,请参阅 (What is the shortest function for reading a cookie by name in JavaScript?)

    并从 input#saveCookie 中移除 onclick 参数

    【讨论】:

    • 那么我的问题是,为什么文本输入有效,但单选按钮无效?这一切都在相同的形式和功能内。此外,当我将 window.location 添加到我的函数底部时,什么都没有保存。
    猜你喜欢
    • 2012-01-03
    • 2018-03-18
    • 1970-01-01
    • 2012-12-19
    • 1970-01-01
    • 1970-01-01
    • 2012-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多