Javascript 在$.each中返回false在$.each中返回false
我使用$.each在$.each中遍历两个数组,我希望在第一个$中返回false。当第二个$中发生事件时,每个都返回false。我应该怎么做Javascript 在$.each中返回false在$.each中返回false,javascript,jquery,Javascript,Jquery,我使用$.each在$.each中遍历两个数组,我希望在第一个$中返回false。当第二个$中发生事件时,每个都返回false。我应该怎么做 $.each(dbFields, function(index, valuedb) { $.each(editFields, function(index, value2) { if ((valuedb.region == value2.regionField) && (valuedb.dbtype == value
$.each(dbFields, function(index, valuedb) {
$.each(editFields, function(index, value2) {
if ((valuedb.region == value2.regionField) && (valuedb.dbtype == value2.typeField)) {
$('#' + value2.idField).attr('id', valuedb.dbid) ;
$('#' + valuedb.dbid).attr('elementid', valuedb.dbid) ;
console.log('Match found between ' + value2.idField + ' and '+valuedb.dbid);
return false; //return false for the first $.each
}
else {
return true ; //return truefor the first $.each
}
});
});
现在,当事件返回false时,它将为第二个$返回false。每个设置一个标志,以便外部循环知道返回false:
$.each(dbFields, function(index, valuedb) {
var flag = true; // <=============
$.each(editFields, function(index, value2) {
if ((valuedb.region == value2.regionField) && (valuedb.dbtype == value2.typeField)) {
$('#' + value2.idField).attr('id', valuedb.dbid);
$('#' + valuedb.dbid).attr('elementid', valuedb.dbid);
console.log('Match found between ' + value2.idField + ' and ' + valuedb.dbid);
flag = false; // <=============
return false; //return false for the first $.each
} /* You don't need this ==============> else {
return true; //return truefor the first $.each
} */
});
return flag; // <=============
});
$。每个(数据库字段、函数(索引、值数据库){
var flag=true;//设置一个标志,以便外部循环知道返回false:
$.each(dbFields, function(index, valuedb) {
var flag = true; // <=============
$.each(editFields, function(index, value2) {
if ((valuedb.region == value2.regionField) && (valuedb.dbtype == value2.typeField)) {
$('#' + value2.idField).attr('id', valuedb.dbid);
$('#' + valuedb.dbid).attr('elementid', valuedb.dbid);
console.log('Match found between ' + value2.idField + ' and ' + valuedb.dbid);
flag = false; // <=============
return false; //return false for the first $.each
} /* You don't need this ==============> else {
return true; //return truefor the first $.each
} */
});
return flag; // <=============
});
$。每个(数据库字段、函数(索引、值数据库){
var flag=true;//不需要else
块。返回true
只会继续each()
的默认行为。不需要else
块。返回true
只会继续each()的默认行为
。它有效谢谢,我不知道我怎么没有想到这样一个简单的解决方案,谢谢伙计。它有效谢谢,我不知道我怎么没有想到这样一个简单的解决方案,谢谢伙计。