加载ajax调用和我的问题

loading for ajax call and my problem with it

本文关键字:我的 问题 调用 ajax 加载      更新时间:2023-09-26

为什么空值后$(idinput).val() == ''不隐藏加载ajax调用#loadingDiv:

    $('#loadingDiv')
        .hide()  // hide it initially
        .ajaxStart(function() {
            $(this).fadeIn('slow');
        })
        .ajaxStop(function() {
            $(this).fadeOut('slow');
        });
    $('.auto_complete').keyup(function () {
            var specific = '.' + $(this).closest('div.auto_box').find('b').attr('class');
            //var cl_list = '.' + $(this).closest('div.auto_box').find('ul').attr('class');        
            var id = '#' + this.id;
            var url = $(id).attr('class');
            var idinput = '#'+$(specific + ' input').attr('id');
            var dataObj = $(this).closest('form').serialize();
            $.ajax({
                type: "POST",
                dataType: 'json',
                url: url,
                data: dataObj,
                cache: false,            
                success: function (data) {
                    //alert(url)
                    var cl_list = '.' + $('.auto_box '+ specific +' ul').attr('class');
                    var id_name = $(cl_list).attr('id');
                    $(cl_list).show().html('');
                    if (data == 0) {
                        $(cl_list).show().html('<p><b>وجود ندارد</b></p>');
                    }
                    else {
                        $.each(data, function (a, b) {
                            //alert(b.name)
                            $('<p id="' + b.name + '">' + b.name + '</p>').appendTo(cl_list);
                        });
                        $(cl_list + ' p').click(function (e) {
                            e.preventDefault();
                            var ac = $(this).attr('id');
                            $('<b>' + ac + '، <input type="text" name="'+id_name+'[]" value="' + ac + '" style="border: none; display: none;" /></b>').appendTo($('.auto_box ' + specific + ' span'));
                            $(this).remove();
                            return false;
                        });
                        $('.auto_box span b').live('click', function (e) {
                            e.preventDefault();
                            $(this).remove();
                            return false;
                        });
                    }
   ///////////////////////////////////here/////////////////////////////////////////////
                        if ($(idinput).val() == '') {
                            alert('cl_list')
                            $(cl_list).hide()
                            $(cl_list + " p").hide().remove();
                            $('.auto_complete').val('');
                            $(".list_name").show().html('');
                        };
   ///////////////////////////////////end//////////////////////////////////////////////
                    $('body').click(function () {
                        $(cl_list + " p").hide().remove();
                        $('.auto_complete').val('');
                        $(cl_list).show().html('');
                        $(cl_list).css('display','none')
                    });
                },
                "error": function (x, y, z) {
                    // callback to run if an error occurs
                    alert("An error has occured:'n" + x + "'n" + y + "'n" + z);
                }
            });
        });

看起来您在语句末尾缺少了几个分号。试试这个:

$('#loadingDiv')
    .hide()  // hide it initially
    .ajaxStart(function() {
        $(this).fadeIn('slow');
    })
    .ajaxStop(function() {
        $(this).fadeOut('slow');
    });
$('.auto_complete').keyup(function () {
        var specific = '.' + $(this).closest('div.auto_box').find('b').attr('class');
        //var cl_list = '.' + $(this).closest('div.auto_box').find('ul').attr('class');        
        var id = '#' + this.id;
        var url = $(id).attr('class');
        var idinput = '#'+$(specific + ' input').attr('id');
        var dataObj = $(this).closest('form').serialize();
        $.ajax({
            type: "POST",
            dataType: 'json',
            url: url,
            data: dataObj,
            cache: false,            
            success: function (data) {
                //alert(url);
                var cl_list = '.' + $('.auto_box '+ specific +' ul').attr('class');
                var id_name = $(cl_list).attr('id');
                $(cl_list).show().html('');
                if (data == 0) {
                    $(cl_list).show().html('<p><b>وجود ندارد</b></p>');
                }
                else {
                    $.each(data, function (a, b) {
                        //alert(b.name);
                        $('<p id="' + b.name + '">' + b.name + '</p>').appendTo(cl_list);
                    });
                    $(cl_list + ' p').click(function (e) {
                        e.preventDefault();
                        var ac = $(this).attr('id');
                        $('<b>' + ac + '، <input type="text" name="'+id_name+'[]" value="' + ac + '" style="border: none; display: none;" /></b>').appendTo($('.auto_box ' + specific + ' span'));
                        $(this).remove();
                        return false;
                    });
                    $('.auto_box span b').live('click', function (e) {
                        e.preventDefault();
                        $(this).remove();
                        return false;
                    });
                }
                if ($(idinput).val() == '') {
                    alert('cl_list');
                    $(cl_list).hide();
                    $(cl_list + " p").hide().remove();
                    $('.auto_complete').val('');
                    $(".list_name").show().html('');
                };
                $('body').click(function () {
                    $(cl_list + " p").hide().remove();
                    $('.auto_complete').val('');
                    $(cl_list).show().html('');
                    $(cl_list).css('display','none');
                });
            },
            "error": function (x, y, z) {
                // callback to run if an error occurs
                alert("An error has occured:'n" + x + "'n" + y + "'n" + z);
            }
        });
    });

尝试:

if(!$(idinput).val())

我发誓我也有同样的错误,但找不到项目。