简体   繁体   English

加载div以使ajax成功

[英]Load div for ajax success

This has been asked before and I did look around, tried many methods but it does not execute. 之前有人问过这个问题,我确实四处看看,尝试了很多方法,但是没有执行。 Only an alert function seems to work. 似乎只有alert功能起作用。 I am trying to load a small div on success: after executing my POST in ajax. 我正在尝试success:加载一个小div success:在ajax中执行POST之后。 Here is my code: 这是我的代码:

$("#send").click(function(e) { e.preventDefault();

          $.ajax({
            type: 'POST',
            url: 'actions/update.php',
            data: $('#content').serialize(),
            success: function() {
          $(this).html('<div id="status">Saved!</div>');
          }
          });

        });

      $('#status').delay(5000).fadeOut(400);

My CSS to this Div: 我对这个Div的CSS:

#status {
position:fixed;
top:50%;
left:50%;
z-index:100000;
width:150px;
height:150px;
margin:-75px 0 0 -75px;
background-color:rgba(0,0,0,0.7);
border-radius:150px;
font-size:30px;
line-height:150px;
font-weight:700;
color:#fff;
text-align:center;
cursor:default; }

Could I be causing conflict somewhere? 我可以在某处引起冲突吗? Is this because e.preventDefault(); 这是因为e.preventDefault(); Any help or link would be appreciated! 任何帮助或链接将不胜感激!

$("#send").click(function(e) { e.preventDefault();
          var that = $(this);
          $.ajax({
            type: 'POST',
            url: 'actions/update.php',
            data: $('#content').serialize(),
            success: function() {
          that.html('<div id="status">Saved!</div>');

          $('#status').delay(5000).fadeOut(400);
          }
          });

        });

The issue here is that this is not what you think it is inisde the callback. 这里的问题是, this不是您认为属于回调的内容。 Try the above 试试上面

Edit: In response to your comment: 编辑:针对您的评论:

The text is overwritten by the new div . 文本被新的div覆盖。 One way to get around that is to insert the new div into a child element of the original element instead. 解决该问题的一种方法是将新的div插入原始元素的子元素中。 To get the fading effect you have to put that into the callback as well. 为了获得淡入淡出的效果,您还必须将其放入回调中。 Otherwise it will execute before the ajax call returns. 否则,它将在ajax调用返回之前执行。 This is because ajax calls are asynchronous and the rest of the JavaScript code will continue to execute while you're waiting for the server to return the data 这是因为ajax调用是异步的,并且在等待服务器返回数据时,其余的JavaScript代码将继续执行

Edit2: I am not sure what type of element you're clicking, but there are two approaches here: Edit2:我不确定您要单击哪种类型的元素,但是这里有两种方法:

1) Add a child element into the clicked element ex: <a #id="send"><div id=someElement></div></a> 1)在点击的元素中添加一个子元素,例如: <a #id="send"><div id=someElement></div></a>

2) Add an element next to the clicked element and target that with the new div 2)在所单击的元素旁边添加一个元素,并使用新的div定位该元素

Inside an $.ajax callback the context( this ) is by default the jqXHR object returned by $.ajax. 在$ .ajax回调内部,context( this )默认是$ .ajax返回的jqXHR对象。
However you can use the context property in the configuration object for the $.ajax method to set what ever you want it to be. 但是,您可以在$ .ajax方法的配置对象中使用context属性来设置所需的内容。 eg 例如

  $.ajax({
    type: 'POST',
    url: 'actions/update.php',
    data: $('#content').serialize(),
    context: this, //<----
    success: function() {
      $(this).html('<div id="status">Saved!</div>');
    }
  });

This will set the context in all callbacks to the one in the calling function. 这会将所有回调中的上下文设置为调用函数中的上下文。

$("#send").click(function(e) { 
    e.preventDefault();
    $.ajax({
        type: 'POST',
        url: 'actions/update.php',
        data: $('#content').serialize(),
    }).done(function() {
        $('#status').html('Saved!');
    });
$('#status').delay(5000).fadeOut(400);
<div id='status'></div>

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

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