退出时,如果条件返回false
我有一个孩子的功能alertMSGS所以alertMSGS功能检查中的四个变量的值,并给出一个警告消息,他们是不相等的,所以它会返回false父功能关闭功能,是DoSave()它的一个闭包函数,从我的知识(Begginer to Javascript)。所以如果返回是错误的,我需要退出保存,否则需要执行保存。 !见下文 。 许多感谢提前退出时,如果条件返回false
function alertMSGS(){
var currentWeekEarning = parseFloat($("#_fid_19").val());
var totalYearEarning= parseFloat($("label[for=_fid_39]").next("div").text().replace('$', '').replace(',', ''));
var currentLoss = parseFloat($("#_fid_20").val());
var totalYearLoss= parseFloat($("label[for=_fid_38]").next("div").text().replace('$', '').replace(',', ''))
if((currentWeekEarning != totalYearEarning)){
alert("currentWeekEarning Not Equal to totalYearEarning");
return false;
}
if (currentLoss != totalYearLoss) {
alert(' currentLoss Not Equal to totalYearLoss');
return false;
}
}
DoSave = (function(fn){
return function(){
var resultofalertMSGS = alertMSGS();
if (resultofalertMSGS === false) {
// so if the function returns false then i need to exit from the (DoSave Function) and if it satisfies then i need to perform the (DoSave Function)
}
var result=fn.apply(fn, arguments);
return result;
}
})(DoSave);
这只是一个想法,你可以实现它。尝试。
DoSave = (function(fn){
var resultofalertMSGS = alertMSGS();
if (resultofalertMSGS) {
(DoSave); // do here what u want to do for saving data
}
var result=fn.apply(fn, arguments); // decide whether it should come inside if or else
return result;
}
})
我收到错误fn.apply不是函数 –
实际上是在那里在你的代码。我完全不知道为什么以及如何使用该功能。 – Sindhoor
但它工作正常,当我的console.log试过我得到的回报为假。所以最后想到的只是退出DoSave功能。你可以提出任何alernative方式 –
稍微更改了代码,为我工作。任何方式谢谢
DoSave = (function(fn){
return function(){
var resultofalertMSGS = alertMSGS();
if (resultofalertMSGS === false) {
return false;
}else{
var result=fn.apply(fn, arguments);
return result;
}
}
})(DoSave);
对不起,但是,你为什么要尝试做这么复杂的事情?你不能在'alertMSGS'函数外面写'DoSave'函数并调用它吗? – Fefux
是啊,我同意fefux – Sindhoor