如何在

时间:2015-12-16 06:08:44

标签: javascript

我想在条件中删除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:{................},为此,我使用了以下代码:

if ($field1=0) {
    $(':contains("field1:{.....};")').each(function(){
        $(this).html($(this).html().split("field1:{......},").join(""));
    });
}

代码无法在条件下删除所需的部件。请帮我解决一下。

1 个答案:

答案 0 :(得分:0)

你必须选择:

delete fields.field1;
fields.field1 = undefined;
相关问题