Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery 如果所有输入字段都是脏的,如何返回true?_Jquery - Fatal编程技术网

Jquery 如果所有输入字段都是脏的,如何返回true?

Jquery 如果所有输入字段都是脏的,如何返回true?,jquery,Jquery,如果表单中的所有字段都是脏字段,则返回true;如果不是,则返回false。 这是我的密码 function getHasChanges() { var hasChanges = false; $(":input:not(:button):not([type=hidden])").each(function () { if ((this.type == "text" || this.type == "textarea" || this.type == "hidd

如果表单中的所有字段都是脏字段,则返回true;如果不是,则返回false。 这是我的密码

function getHasChanges() {
    var hasChanges = false;

    $(":input:not(:button):not([type=hidden])").each(function () {
        if ((this.type == "text" || this.type == "textarea" || this.type == "hidden") && this.defaultValue != this.value) {
            hasChanges = true;
            return false;             }
        else {
            if ((this.type == "radio" || this.type == "checkbox") && this.defaultChecked != this.checked) {
                hasChanges = true;
                return false;                 }
            else {
                if ((this.type == "select-one" || this.type == "select-multiple")) {
                    for (var x = 0; x < this.length; x++) {
                        if (this.options[x].selected != this.options[x].defaultSelected) {
                            hasChanges = true;
                            return false;
                        }
                    }
                }
            }
        }
    });

    return hasChanges;
}
函数getHasChanges(){ var hasChanges=false; $(“:输入:非(:按钮):非([type=hidden]))。每个(函数(){ if((this.type==“text”| | this.type==“textarea”| | this.type==“hidden”)&&this.defaultValue!=this.value){ hasChanges=true; 返回false;} 否则{ if((this.type==“radio”| | this.type==“checkbox”)&&this.defaultChecked!=this.checked){ hasChanges=true; 返回false;} 否则{ 如果((this.type==“选择一个”| | this.type==“选择多个”)){ 对于(var x=0;x 我使用了这段代码并试图对其进行操作,但我无法理解


我的问题是:如何检查表单上的所有字段是否都是脏的?

因为您想知道所有字段是否都已更改,我将颠倒逻辑。初始化hasChanges为true,并在没有更改时立即设置为false

像这样:

function getHasChanges() {
    var hasChanges = true;

    $(":input:not(:button):not([type=hidden])").each(function () {
        if ((this.type == "text" || this.type == "textarea" || this.type == "hidden") && this.defaultValue == this.value) {
            hasChanges = false;
        }
        else if ((this.type == "radio" || this.type == "checkbox") && this.defaultChecked == this.checked) {
            hasChanges = false;
        }
        else if ((this.type == "select-one" || this.type == "select-multiple")) {
            for (var x = 0; x < this.length; x++) {
                if (this.options[x].selected == this.options[x].defaultSelected) {
                    hasChanges = false;
                }
            }
        }
    });

    return hasChanges;
}
函数getHasChanges(){ var hasChanges=true; $(“:输入:非(:按钮):非([type=hidden]))。每个(函数(){ if((this.type==“text”| | this.type==“textarea”| | this.type==“hidden”)&&this.defaultValue==this.value){ hasChanges=false; } else if((this.type==“radio”| | this.type==“checkbox”)&&this.defaultChecked==this.checked){ hasChanges=false; } else if((this.type==“选择一个”| | this.type==“选择多个”)){ 对于(var x=0;x如果您想知道所有字段是否都已更改,我将颠倒逻辑。初始化hasChanges为true,并在没有更改时立即设置为false

像这样:

function getHasChanges() {
    var hasChanges = true;

    $(":input:not(:button):not([type=hidden])").each(function () {
        if ((this.type == "text" || this.type == "textarea" || this.type == "hidden") && this.defaultValue == this.value) {
            hasChanges = false;
        }
        else if ((this.type == "radio" || this.type == "checkbox") && this.defaultChecked == this.checked) {
            hasChanges = false;
        }
        else if ((this.type == "select-one" || this.type == "select-multiple")) {
            for (var x = 0; x < this.length; x++) {
                if (this.options[x].selected == this.options[x].defaultSelected) {
                    hasChanges = false;
                }
            }
        }
    });

    return hasChanges;
}
函数getHasChanges(){ var hasChanges=true; $(“:输入:非(:按钮):非([type=hidden]))。每个(函数(){ if((this.type==“text”| | this.type==“textarea”| | this.type==“hidden”)&&this.defaultValue==this.value){ hasChanges=false; } else if((this.type==“radio”| | this.type==“checkbox”)&&this.defaultChecked==this.checked){ hasChanges=false; } else if((this.type==“选择一个”| | this.type==“选择多个”)){ 对于(var x=0;x我认为你做错了。到处都返回false,因此无论是否有更改,输出都将始终为false。即使您有
hasChanges=true返回
false
更改代码:

function getHasChanges() {
    var hasChanges = false;

    $(":input:not(:button):not([type=hidden])").each(function () {
        if ((this.type == "text" || this.type == "textarea" || this.type == "hidden") && this.defaultValue != this.value) {
            hasChanges = true;
         }
        else 
         {
            if ((this.type == "radio" || this.type == "checkbox") && this.defaultChecked != this.checked) {
                hasChanges = true;
             }
            else 
            {
                if ((this.type == "select-one" || this.type == "select-multiple")) {
                    for (var x = 0; x < this.length; x++) {
                        if (this.options[x].selected != this.options[x].defaultSelected) {
                            hasChanges = true;
                       }
                    }
                }
            }
        }
    });

    return hasChanges;
}
函数getHasChanges(){ var hasChanges=false; $(“:输入:非(:按钮):非([type=hidden]))。每个(函数(){ if((this.type==“text”| | this.type==“textarea”| | this.type==“hidden”)&&this.defaultValue!=this.value){ hasChanges=true; } 其他的 { if((this.type==“radio”| | this.type==“checkbox”)&&this.defaultChecked!=this.checked){ hasChanges=true; } 其他的 { 如果((this.type==“选择一个”| | this.type==“选择多个”)){ 对于(var x=0;x
编辑:

即使上面的代码也会有问题;因为一旦我们将其设置为true,即使下面查询中的字段没有更改,我们也不会将其设置为false。您需要使用这个:

function getHasChanges() {
            var hasChanges = false;

            $(":input:not(:button):not([type=hidden])").each(function () {
                if ((this.type == "text" || this.type == "textarea" || this.type == "hidden")) {
                    if(this.defaultValue != this.value) {
                        hasChanges = true;
                    } else {
                        hasChanges = false;
                    }
                    return hasChanges;
                 } else {
                    if ((this.type == "radio" || this.type == "checkbox")) {
                        if(this.defaultChecked != this.checked) {
                            hasChanges = true;
                        } else {
                            hasChanges = false;
                        }
                     }
                     return hasChanges;
                    else 
                    {
                        if ((this.type == "select-one" || this.type == "select-multiple")) {
                            for (var x = 0; x < this.length; x++) {
                                if (this.options[x].selected != this.options[x].defaultSelected) {
                                    hasChanges = true;
                                }
                            }
                        }
                        return hasChanges;
                    }
                }
            });

            return hasChanges;
    }
函数getHasChanges(){ var hasChanges=false; $(“:输入:非(:按钮):非([type=hidden]))。每个(函数(){ if((this.type==“text”| | this.type==“textarea”| | this.type==“hidden”)){ if(this.defaultValue!=this.value){ hasChanges=true; }否则{ hasChanges=false; } 回报与变化; }否则{ if((this.type==“radio”| | this.type==“checkbox”)){ 如果(this.defaultChecked!=this.checked){ hasChanges=true; }否则{ hasChanges=false; } } 回报与变化; 其他的 { 如果((this.type==“选择一个”| | this.type==“选择多个”)){ 对于(var x=0;xfunction getHasChanges() { var hasChanges = false; $(":input:not(:button):not([type=hidden])").each(function () { if ((this.type == "text" || this.type == "textarea" || this.type == "hidden") && this.defaultValue != this.value) { hasChanges = true; else { if ((this.type == "radio" || this.type == "checkbox") && this.defaultChecked != this.checked) { hasChanges = true; else { if ((this.type == "select-one" || this.type == "select-multiple")) { for (var x = 0; x < this.length; x++) { if (this.options[x].selected != this.options[x].defaultSelected) { hasChanges = true; } } } } } }); return hasChanges; }
function getHasChanges() {
    var incomplete = false;

    var ch = {
      text: {
            list: ['text', 'textarea', 'hidden'],
            defaultProp: 'defaultValue',
            valueProp: 'value'
        },
      check: {
            list: ['radio', 'checkbox'],
            defaultProp: 'defaultChecked',
            valueProp: 'checked'
        },
      select: {
            list: ['select-one', 'select-multiple'],
            defaultProp: 'defaultSelected',
            valueProp: 'selected',
            multi: 'options'
        }
    };

    $(':input:not(:button):not([type=hidden])').each(function () {
        for(kind in ch){
            for(k in ch[kind].list){
                var type = ch[kind].list[k];
                if(this.type == type){
                    if(ch[kind].multi){
                        for (var i = 0; i < this.length; i++) {
                            if(type == 'select-one'){
                                // for single select the default needs to have changed 
                                if(this[ch[kind].multi][i][ch[kind].defaultProp] && this[ch[kind].multi][i][ch[kind].valueProp]){
                                    // this defaulted to selected and is still selected
                                    incomplete = true;
                                    return;
                                }
                            } else if(type == 'select-multiple'){
                                // it's not clear what's correct here.
                                // do you want each selected item to change?
                                // for now do nothing.
                            }
                        }
                    } else {
                        if(this[ch[kind].valueProp] === '' || this[ch[kind].defaultProp] == this[ch[kind].valueProp]){
                            incomplete = true;
                            return;
                        }
                    }
                    break;
                }
            }
        }
    });

    return incomplete;
}