简体   繁体   English

在本地存储中存储复选框

[英]Storing check box in local storage

I'm able to store text how I want but cant get check boxes to work properly. 我能够按需要存储文本,但无法使复选框正常工作。 It is storing the check box values as 'on' regardless if checked or not. 无论是否选中,它将复选框值存储为“ on”。 How can I set is to store "off" by default and "on" when checked? 如何设置默认情况下存储为“关闭”而选中时为“打开”?

something that would achieve this: 可以达到此目的的东西:

var value = $(this).val() && $(this).prop(checked);

JS Fiddle: http://jsfiddle.net/cRJ23/17/ JS小提琴: http : //jsfiddle.net/cRJ23/17/

Storage.prototype.setObj = function (key, obj) {
    return this.setItem(key, JSON.stringify(obj))
}
Storage.prototype.getObj = function (key) {
    return JSON.parse(this.getItem(key))
}

var Survey = {},
    Surveys = {},
    save = function () {
        $('input, select, textarea').each(function () {
            var value = $(this).val(),
                name = $(this).attr('name');
            console.log('Saving');
            console.log(name + ':' + value);
            Survey[name] = value;
        });

        if (localStorage.getObj('Surveys') != null) {
            Surveys = localStorage.getObj('Surveys');
        }
        Surveys[$('#FirstName').val() + '.' + $('#LastName').val()] = Survey; //store in big list
        localStorage.setObj('Surveys', Surveys);

    }

Hope this helps: 希望这可以帮助:

var value = $(this).val();
var   name = $(this).attr('name');

if($(this).hasClass('checkers')){
    value = $(this).is(":checked")
    if(value){
        value='on';
    }else{
        value='off';
    }
}

http://jsfiddle.net/juvian/cRJ23/22/ http://jsfiddle.net/juvian/cRJ23/22/

You should not use .val to check whether checkbox is selected, use .is('checked') 您不应使用.val检查是否已选中复选框,而应使用.is('checked')

Instead of using .val() to get the state of a checkbox you should use .prop() or the property .checked 代替使用.val()获取复选框的状态,您应该使用.prop().checked属性

   $('input, select, textarea').each(function () {
        var value = ($(this).is('[type="checkbox"]')) ? this.checked :$(this).val(),
            name = $(this).attr('name');
        console.log('Saving');
        console.log(name + ':' + value);
        Survey[name] = value;
    });

Working example: http://jsfiddle.net/cRJ23/24/ 工作示例: http : //jsfiddle.net/cRJ23/24/

Working Fiddle http://jsfiddle.net/jFeLv/ 工作小提琴 http://jsfiddle.net/jFeLv/

The val function does not work like you would expect with check boxes, the way to get true or false from them is to use the is(':checked') function. val函数不能像您期望的那样使用复选框,从中获取真或假的方法是使用is(':checked')函数。

$(this).is(':checked')

The fiddle is a modified version of your code by modifying one line that seems to be working fine by shorthanding an if else for the variable "value" checking to see if it is a checkbox. 小提琴是代码的修改版本,方法是修改一行代码,方法是通过简化变量“ value”的if else来简化检查,以查看其是否为复选框。

($(this).attr('type','checkbox') ? value = $(this).val() : value = $(this).is(':checked'));

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM