如果表单中的所有字段为脏,则返回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;
}
我使用了这段代码并试图操纵它,但我无法理解它。
我的问题是:如何检查表单上的所有字段是否都是脏的?
答案 0 :(得分:2)
我认为你做错了。您在任何地方都返回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;
}
修改强>
即使上面的代码也会有问题;因为一旦我们将其设置为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;
}
希望这会有所帮助:)
错过了一个支架。请检查一下:
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;
}
答案 1 :(得分:1)
如果您想知道所有字段是否已更改,我会反转逻辑。将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;
}
答案 2 :(得分:1)
这是我的刺:
如果所有字段都具有非默认的非空字符串值,则返回false
。如果所有字段都设置为默认值或为空字符串,则返回true。
注意:即使语法错误也没有测试过,所以YMMV。
编辑:已更新:已检查语法,现在就运行。
编辑2 :再次更新以更智能地考虑选择框。目前尚不清楚多选应如何处理,但你原来的方式肯定会产生意想不到的结果。
供参考,my jsbin为此。
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;
}
答案 3 :(得分:0)
问题是您设置了hasChanges = true
,然后设置了return 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;
}