如何在单击复选框时在 jquery 中存储 cookie 值(可以有多个值)

发布于 2024-12-01 05:09:46 字数 403 浏览 0 评论 0原文

让我们假设我有一个文本框,可以在其中输入用逗号分隔的用户名。

详细信息:

  1. 我附近有一个文本框和一个搜索按钮。

  2. 当我单击搜索框时,用户列表将显示在弹出窗口中,附近有一个复选框

  3. 当用户点击复选框并点击提交时,相应的用户名将显示在文本框中。

  4. 我的问题是:我需要存储我单击的复选框的每个值,并使用 jquery 将其存储在 cookie 中。 *

     ** 可以存储用逗号分隔的多个值。 **
     ** 不应有重复。 **
    

任何帮助将不胜感激......

提前致谢......

Let us consider i am having a text box where i can enter user names separated by commas.

In Detail:

  1. I am having a text box and a search_button nearby.

  2. When i click on the search box list of users will be displayed there in a popup with a check box nearby

  3. When the user the clicks the check box and clicks submit the corresponding users name will be displayed on the text box.

  4. My problem is: I need to store every value of the check box which i clicked and to store it in cookie using jquery. *

     ** There is the possible of storing multiple values separated by commas. **
     ** There should not be duplicates. **
    

Any help will be thankful and grateful....

Thanks in advance...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

未蓝澄海的烟 2024-12-08 05:09:46

您好,您可以使用这个插件 cookie 来做到这一点(见下文),
您可以执行以下操作:

var App = {
count:0,

clickedCheckbox:function(e)
{
    $.cookie('userCheckValue_'+this.count, e.target.value);
    this.count++;
}

}

并为特定键实现更多功能,检查是否已经存在某个值
ETC ...

jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
    options = options || {};
    if (value === null) {
        value = '';
        options.expires = -1;
    }
    var expires = '';
    if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
        var date;
        if (typeof options.expires == 'number') {
            date = new Date();
            date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
        } else {
            date = options.expires;
        }
        expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
    }
    // CAUTION: Needed to parenthesize options.path and options.domain
    // in the following expressions, otherwise they evaluate to undefined
    // in the packed version for some reason...
    var path = options.path ? '; path=' + (options.path) : '';
    var domain = options.domain ? '; domain=' + (options.domain) : '';
    var secure = options.secure ? '; secure' : '';
    document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

};

Hello you can do that with this plugin cookie (See below),
with that you can do something like:

var App = {
count:0,

clickedCheckbox:function(e)
{
    $.cookie('userCheckValue_'+this.count, e.target.value);
    this.count++;
}

}

And implement more function for the specific key, check if already exists some value
etc ...

jQuery.cookie = function(name, value, options) {
if (typeof value != 'undefined') { // name and value given, set cookie
    options = options || {};
    if (value === null) {
        value = '';
        options.expires = -1;
    }
    var expires = '';
    if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
        var date;
        if (typeof options.expires == 'number') {
            date = new Date();
            date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
        } else {
            date = options.expires;
        }
        expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
    }
    // CAUTION: Needed to parenthesize options.path and options.domain
    // in the following expressions, otherwise they evaluate to undefined
    // in the packed version for some reason...
    var path = options.path ? '; path=' + (options.path) : '';
    var domain = options.domain ? '; domain=' + (options.domain) : '';
    var secure = options.secure ? '; secure' : '';
    document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
} else { // only name given, get cookie
    var cookieValue = null;
    if (document.cookie && document.cookie != '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}

};

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文