开发者

jQuery returns undefined

开发者 https://www.devze.com 2023-02-28 22:49 出处:网络
I have a really basic problem $(\'#myForm\').submit(function() { var v= input_value(this,\'validate_code\');

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

精彩评论

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

关注公众号