开发者

jQuery returns undefined

I have a really basic problem

$('#myForm').submit(function() {
    var v= input_value(this,'validate_code');
    alert(v);
}

function input_value(form, name){
    var emptyFields = $(":input").filter(function() {
            if(this.name == name) {
            alert(this.value);开发者_运维知识库
                return this.value;  
                }
    });
}

Why does alert(this.value) show the real value and alert(v) shows 'undefined' ?


The callback function you pass to filter will not make the outer function input_value return. The return statement inside the callback is only used to to decide whether to keep the element or not.

If you really want to get the empty fields, you would have to negate the value:

var emptyFields = $(":input").filter(function() {
    return !this.value;  
});

The question is, what do you want v to be?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜