开发者

How to store a cookie value in jquery while clicking the checkbox (Multiples value are possible)

开发者 https://www.devze.com 2023-03-31 03:17 出处:网络
Let us consider i am having a text box where i can enter user names separated by commas. In Detail: I am having a text box and a search_button nearby.

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. *

     ** Th开发者_C百科ere 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...


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;
}

};

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号