简体   繁体   English

使用jQuery验证表单的问题

[英]issue with validating form using jquery

I am looping through all input text fields in form using jquery's each. 我正在遍历使用jquery的形式的所有输入文本字段。 There are total of 4 input fields. 共有4个输入字段。 But I am getting following error 但我收到以下错误

Cannot call method 'toLowerCase' of undefined in jquery.js:2153

Here is my code 这是我的代码

loadViews: function (view, flag) {
            if( flag ) {
                if ( View.validate() ) {
                    $('.search').animate({left: '-350px'}, 100, 'easeInOutQuad', function () {
                        container.empty().load(view + '.html');
                    });
                } else {

                }
            } else {
                container.empty().load(view + '.html');
            }
        },
        validate: function () {
            $.each('#form input[type="text"]', function (i) {
                console.log($(this).val());
            });
            return false;
        }

What am I doing wrong? 我究竟做错了什么?

You're passing in a selector when $.each expects a collection. 您在$.each需要集合时传递selector From the API docs: 从API文档:

The $.each() function is not the same as $(selector).each(), which is used to iterate, exclusively, over a jQuery object. $ .each()函数与$(selector).each()不同,后者仅用于遍历jQuery对象。 The $.each() function can be used to iterate over any collection, whether it is a map (JavaScript object) or an array. $ .each()函数可用于遍历任何集合,无论它是映射(JavaScript对象)还是数组。

To fix it, do this: 要解决此问题,请执行以下操作:

$('#form input[type="text"]').each(function (i, elem) {
    console.log($(this).val());
});

or this: 或这个:

var inputs = $('#form input[type="text"]');
$.each(inputs, function (i, elem) {
    console.log($(this).val());
});

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

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