简体   繁体   English

返回布尔值更高的函数链

[英]return boolean value higher up the chain in a function

I have the following function in jQuery : 我在jQuery中具有以下功能:

function vali(id) {
    $(id).bind("jqv.field.result", function(event, field, errorFound, prompText) {
        $(id).removeClass('validationError');
        if(errorFound) {
          $(id).addClass('validationError');
          return false;
        };
        return true;
    });
}

I am adding a validation class to inputs in a complex form to style it appropriately when an input is invalid. 我正在以复杂的形式向输入中添加一个验证类,以在输入无效时适当地设置其样式。 I would also like to return a true or false value based on this. 我也想基于此返回true或false值。 The ' validationError ' class is being added correctly to inputs but I cannot return the boolean value as I suspect that I am calling a function within a function here and therefore need to return the value to the higher order function? validationError ”类已正确添加到输入中,但是我无法返回布尔值,因为我怀疑我在这里在函数内调用函数,因此需要将值返回给高阶函数吗? Something like " return.return false; " Obviously this doesn't work but you get what I'm thinking on this. 诸如“ return.return false; ”之类的东西显然是行不通的,但是您得到了我对此的想法。 Maybe I am way off? 也许我走了吗?

If I reference a specific input say for eg: 如果我引用特定的输入,例如:

console.log( vali('#cause-name') ); 

the validationError styling works but I get 'undefined' in the console. validateError样式可以工作,但在控制台中出现“未定义”。 Thanks 谢谢

This should work: 这应该工作:

function vali(id) {
        var valid = true;
        $(id).bind("jqv.field.result", function(event, field, errorFound, prompText) {
            $(id).removeClass('validationError');
            if(errorFound) {
              $(id).addClass('validationError');
              valid = false;
            };
            valid = true;
        });
        return valid;
    } 

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

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