简体   繁体   English

如何从元素中删除所有jQuery验证引擎类?

[英]How to remove all jQuery validation engine classes from element?

I have a plugin that is cloning an input that may or may not have the jQuery validation engine bound to it. 我有一个插件正在克隆可能绑定或未绑定jQuery验证引擎的输入。

so, it's classes may contain eg validate[required,custom[number],min[0.00],max[99999.99]] or any combination of the jQuery validation engine validators. 因此,它的类可能包含例如validate[required,custom[number],min[0.00],max[99999.99]]或jQuery验证引擎验证器的任意组合。

The only for sure thing is that the class begins with validate[ and ends with ] , but to make it more complicated as in the example above, there can be nested sets of [] . 唯一可以确定的是,该类以validate[开始,以]结尾,但是要使其变得更加复杂(如上例所示),可以嵌套[]集合。

So, my question is, how can I remove these classes (without knowing the full class) using jQuery? 因此,我的问题是,如何使用jQuery删除这些类(不知道完整的类)?

Here is my implementation, It's not using regex, but meh, who said it had too? 这是我的实现,它没有使用正则表达式,但是,嗯,谁也说过呢?

//'first validate[ required, custom[number], min[0.00], max[99999.99] ] other another';
var testString = $('#test')[0].className; 

function removeValidateClasses(classNames) {
    var startPosition = classNames.indexOf("validate["),
        openCount     = 0, 
        closeCount    = 0,
        endPosition   = 0;

    if (startPosition === -1) {
        return;
    }

    var stringStart     = classNames.substring(0, startPosition),
        remainingString = classNames.substring(startPosition),
        remainingSplit  = remainingString.split('');

    for (var i = 0; i < remainingString.length; i++) {
        endPosition++;
        if (remainingString[i] === '[') {
            openCount++;
        } else if (remainingString[i] === ']') {
            closeCount++;
            if (openCount === closeCount) {
                break;
            }
        }
    }

    //concat the strings, without the validation part
    //replace any multi-spaces with a single space
    //trim any start and end spaces
    return (stringStart + remainingString.substring(endPosition)) 
           .replace(/\s\s+/g, ' ')
           .replace(/^\s+|\s+$/g, '');
}
$('#test')[0].className = removeValidateClasses(testString);

It might actually be simpler to do that without JQuery. 如果没有JQuery,这样做实际上可能会更简单。 Using the className attribute, you can then get the list of classes using split(), and check whether the class contains "validate[". 然后,使用className属性,可以使用split()获取类列表,并检查该类是否包含“ validate [”。

var classes = $('#test')[0].className.split(' ');
var newClasses = "";

for(var i = 0; i < classes.length; i++){
    if(classes[i].indexOf('validate[') === -1){
        newClasses += classes[i];
    }
}

$('#test')[0].className = newClasses

I think this solution is even more simple. 我认为这种解决方案更加简单。 You just have to replace field_id with the id of that element and if the element has classes like some_class different_class validate[...] it will only remove the class with validate, leaving the others behind. 您只需field_id该元素的id替换field_id ,并且如果该元素具有诸如some_class different_class validate[...]之类的类,它将仅删除带有validate的类,而将其他类甩在后面。

var that ='#"+field_id+"';
  var classes = $(that).attr('class').split(' ');
  $.each(classes, function(index, thisClass){
 if (thisClass.indexOf('validate') !== -1) {
$(that).removeClass(classes[index])
}
 });

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

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