Jquery错误返回bool值

jquery error with return bool value

本文关键字:bool 返回 错误 Jquery      更新时间:2023-09-26

我是javascript的新手,我不能返回函数的布尔值。我正在验证文本框的null和返回false,如果它是空的帮助请?

validateForm : function() {
    $('.requiredField :input').each(function(){
        if ('input[type=text]'){
            if($(this).val().length === 0){
                    $(this).addClass('warning');
                    var  errorMsg = $('<br /><span>Please Fill in the TextBox</span>').addClass('warning');
                    errorMsg.insertAfter(this);
                    $(errorMsg).css('color','red');
                    $('.warning').css('border-color','red');
            //$(this).focus(function(){
                //$(this).removeClass('warning');   
                //$(this).parent().children('span').remove();
                //$(this).parent().children('br').remove();         
            //});
                    return false;
                }
            else 
                return true;
            }
        }); 
},
Form.validateForm(); // call to the function 

您从 .each()return。这并不会使你的函数返回一个值。

.each()循环中,return false;类似于使用break;, return true;类似于使用continue;

你需要在.each()的外声明一个变量,在循环内设置它的值,然后在循环后返回

检查这一行

if ('input[type=text]'){

应该是

 if($('input[type=text]')){

你可以试试:

$('.requiredField :input').each(function(){
var i=$(this).val();
if(i == '' || i == null)
 {
 //execute your codes
 return false;
 }else{
 return true;
 }
});

似乎你正在尝试写一个插件?试试下面的代码:

(function($) {
    $.fn.validateForm = function()
    {
        var formID = $(this).attr('id');
        $('#'+ formID +' input[type=submit]').click(function(e)
        { 
            e.preventDefault();
            $('input[type=text]').each(function(){
                if($(this).val().length === 0){                 
                        $(this).addClass('warning');
                        var  errorMsg = $('<span>Please Fill in the TextBox</span>').addClass('warning');
                        errorMsg.insertAfter(this);
                        $(errorMsg).css('color','red');
                        $('.warning').css('border-color','red');          
                        return false;
                }else{
                    return true;
                }
            });
        });
    };
})(jQuery);

正如@RocketHazmat所提到的,你的函数需要从内部循环中聚合结果,并有一个单一的出口点,以便验证(并添加css类/html元素)每个输入。

你需要这样做:

validateForm : function () {
        var invalidInputs = [];
        // only test the type='text' inputs
        $('.requiredField :input[type="text"]').each(function () {
            // if there is no value
            if ($(this).val() == undefined || $(this).val().length == 0) {
                $(this).addClass('warning');
                var errorMsg = $('<br /><span>Please Fill in the TextBox</span>').addClass('warning');
                errorMsg.insertAfter(this);
                $(errorMsg).css('color','red');
                $('.warning').css('border-color','red');
                // add the invalid input to an array
                invalidInputs.push($(this));
            }
        }); 
        // The form is only valid for no invalid inputs were found.
        return invalidInputs.length == 0;
    }