如何设置为未定义或删除某些部分

时间:2015-12-16 09:07:06

标签: javascript

我正在尝试几个小时,但找不到删除或未定义的以下代码部分的解决方案:

$(document).ready(function() {
    // Generate a simple captcha
    function randomNumber(min, max) {
        return Math.floor(Math.random() * (max - min + 1) + min);
    };
    $('#captchaOperation').html([randomNumber(1, 100), '+', randomNumber(1, 200), '='].join(' '));

    $('#defaultForm').bootstrapValidator({
        message: 'This value is not valid',
        fields: {
            field1: {
                message: 'The field is not valid',
                validators: {
                    notEmpty: {
                        message: 'The field is required and can\'t be empty'
                    },
                    stringLength: {
                        min: 1,
                        max: 30,
                        message: 'The field must be more than 1 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: 'The field can only consist of alphabetical, number, dot and underscore'
                    }

                }
            },
            field2: {
                message: 'The field is not valid',
                validators: {
                    notEmpty: {
                        message: 'The field is required and can\'t be empty'
                    },
                    stringLength: {
                        min: 1,
                        max: 30,
                        message: 'The field must be more than 1 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: 'The field can only consist of alphabetical, number, dot and underscore'
                    }

                }
            },     
            captcha: {
                validators: {
                    callback: {
                        message: 'Wrong answer',
                        callback: function(value, validator) {
                            var items = $('#captchaOperation').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]);
                            return value == sum;
                        }
                    }
                }
            }
        }
    });
});

在上面的脚本中我想删除field1:{................},为此以下将完成这项工作:

delete fields.field1; or
fields.field1 = undefined;

但我无法理解如何使用这些代码。

2 个答案:

答案 0 :(得分:4)

首先,您需要将对象存储在变量中:

var validation = {
    message: 'This value is not valid',
    fields: {
        field1: {
            message: 'The field is not valid',
            validators: {
                notEmpty: {
                    message: 'The field is required and can\'t be empty'
                },
                stringLength: {
                    min: 1,
                    max: 30,
                    message: 'The field must be more than 1 and less than 30 characters long'
                },
                regexp: {
                    regexp: /^[a-zA-Z0-9_\.]+$/,
                    message: 'The field can only consist of alphabetical, number, dot and underscore'
                }

            }
        },
        field2: {
            message: 'The field is not valid',
            validators: {
                notEmpty: {
                    message: 'The field is required and can\'t be empty'
                },
                stringLength: {
                    min: 1,
                    max: 30,
                    message: 'The field must be more than 1 and less than 30 characters long'
                },
                regexp: {
                    regexp: /^[a-zA-Z0-9_\.]+$/,
                    message: 'The field can only consist of alphabetical, number, dot and underscore'
                }

            }
        },     
        captcha: {
            validators: {
                callback: {
                    message: 'Wrong answer',
                    callback: function(value, validator) {
                        var items = $('#captchaOperation').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]);
                        return value == sum;
                    }
                }
            }
        }
    }
};

然后你可以删除它:

delete validation.fields.field1;

答案 1 :(得分:0)

使用它:

delete fields['field1'];