简体   繁体   English

使用append()函数在ajax成功后显示通知

[英]Showing notification after ajax success with append() function

I want to show a notification after ajax success and failure function. 我想在ajax成功和失败功能后显示通知。 I am showing the notification with append() function and delay function after it appends to the parent div. 我将在追加到父div之后显示带有append()函数和delay函数的通知。 But the problem now is this gets triggered only once. 但是现在的问题是,这仅触发一次。 I want to trigger the notification everytime ajax is done. 我想每次ajax完成时触发通知。

function addDroppedCandidates(data){

var data = JSON.parse(data);
//alert(data.msg.name);

if (data.msg == "no_int") {

    $('#no_int_modal').modal('show');
}
else {

    var name = data.msg.name.substring(0,6);
var profile_name = "{{$profile->profile_name}}"
console.log(data.resume);

$('.notification_panel_parent').append('<div class = "panel notification_panel" id = "">'+

    '<div class = "panel-body">'+
     '<div class="col-lg-4">'+
         '<img id = "notify_file" src="/icons/pdf_success.png">'+
     '</div>'+

        '<div class="col-lg-8 name_and_info">'+

            '<span class = "notify_cand_name">'+resume_name+'</span>'+
            '<span class="file_status">File upload successfull</span>'+

        '</div>'+

    '</div>'+
  '</div>').delay(1200).fadeOut(4000); 
  }
  }

This is the div i appending too. 这也是我附加的div。

 <div class = "notification_panel_parent">
 </div>

How do i trigger it everytime ajax is called ? 每次调用ajax时如何触发它? .notification_panel_parent is above some other div. .notification_panel_parent在其他div之上。 .notification_panel_parent has higher z-index. .notification_panel_parent具有较高的z-index。

.notification_panel{
width: 300px;
height: 90px;
margin-left: 73%;
z-index: 1000000001;
position: absolute;
margin-top: 8%;
box-shadow: 0 0 10px #d4d4d4;
border-radius: 5px;
font-size: 20px;
}

The other problem is when it fades ou,t it goes below other div (z-index is not working here). 另一个问题是,当它逐渐消失时,它会降到其他div以下(z索引在这里不起作用)。

Here is my ajax function 这是我的ajax功能

$.ajax({


        url : '/resume_upload',
        contentType: "application/json",
        type : 'POST',
        headers: {'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')},
        mimeType: "multipart/form-data",
        data : form_data,

        success : function(data) {
        addDroppedCandidates(data);                                       
        },
        processData : false,
        contentType : false,
        error : function(xhr ,status ,error){
        console.log(xhr);
        alert(xhr);
        }

        });
        e.preventDefault();
        e.stopImmediatePropagation();
        }

Instead of fading out, you should do something like 除了淡出淡出,您还应该做类似的事情

.delay(2000).html('')

Or 要么

.delay(2000).fadeOut(3000).html('')

I haven't tried this, so might not work. 我没有尝试过,所以可能行不通。 But try this, if they don't then let me know I'll see what's the exact issue 但是,请尝试一下,如果他们不这样做,请告诉我,我会看到确切的问题是什么


UPDATE UPDATE

Ok so what I am understanding from your question, correct me If I'm wrong. 好吧,我从您的问题中了解的内容,如果我错了,请纠正我。

  • You want to show a notification after each successful AJAX call. 您希望在每次成功的AJAX调用后显示通知。
  • For that, you've created a function which appends a panel in notification_panel_parent id. 为此,您创建了一个函数,该函数appends面板appendsnotification_panel_parent id中。
  • Then you fade out that child panel after 4secs. 然后,在4秒后淡出该子面板。

Now, I'm seeing these issues in this approach, 现在,我以这种方式看到了这些问题,

  • You're appending a new panel each time after a successful AJAX and I'm not sure if you are using your previous appended panel again (let me know if I'm wrong). 在AJAX成功之后,您每次都会添加一个新面板,但我不确定您是否再次使用上一个已添加的面板(如果我错了,请告诉我)。
  • This will add unexpected behaviour as you're going to have multiple child panels in your DOM. 这将增加意外行为,因为您将在DOM中具有多个子面板。

What I believe you can do is create one panel in start to show user and then make its opacity 0. 我相信您可以做的是在开始时创建一个面板来向用户​​显示,然后使其opacity 0。

Then, after every successful AJAX call, you just makes its opacity 1 and after 4 seconds delay, make its opacity 0 again, using fadeIn and faaeOut functions. 然后,在每次成功的AJAX调用之后,只需将其不透明度设置为1,并在延迟4秒后,使用fadeIn and faaeOut函数再次将其不透明度设置为0。

Something like this: 像这样:

 $('#child-panel').css({opacity: 1}); setTimeout(function() { $('#child-panel').delay(1000).fadeOut(2000) },2000); 
 #child-panel { opacity: 0; background:purple;height:75px;width:75px;margin:6px; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="child-panel" style=""></div> 

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

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