【问题标题】:Display message after success of function with ajax使用ajax函数成功后显示消息
【发布时间】:2015-07-29 11:05:01
【问题描述】:

我试图在 ajax 请求完成时显示成功消息,但到目前为止还没有成功。 这是html部分

<a href="#" class="bookmarked has-tooltip" data-title="Add to favorite" id="'.$row['image_id'].'"></a>
<span id="message"></span>

这是ajax部分

$(document).ready(function(){
$('.bookmarked', $('.buttons')).click(function(){
$.post('misc/add_favorites.php', { 
    image_id: $(this).attr('id') }, 
       success: function(){
        $('#message').html('<div id="alertFadeOut" style="color: green">Added to favorites!</div>'); // Diplay message with a fadeout
          $('#alertFadeOut').fadeOut(3000, function () {
              $('#alertFadeOut').text('');
          }); 
        });
    console.log(data);
});

});

在控制台中我也遇到了这个错误

Uncaught SyntaxError: missing ) after argument list

【问题讨论】:

  • 控制台将该错误具体指向何处?
  • 关于成功函数-success: function(){
  • 这是什么? $('.bookmarked', $('.buttons')) ?

标签: jquery ajax


【解决方案1】:

你不要在帖子中使用success,它与ajax不同

<script>    


$(document).ready(function(){
        $('.bookmarked', .buttons').click(function(){
                $.post('misc/add_favorites.php', 
                { 
                    "image_id": $(this).attr('id'),
                },
                 function(){
                $('#message').html('<div id="alertFadeOut" style="color: green">Added to favorites!</div>'); // Diplay message with a fadeout
                  $('#alertFadeOut').fadeOut(3000, function () {
                      $('#alertFadeOut').text('');
                  }); 
              });

            console.log(data);
        });


});


 </script>

看看这可能有用

新编辑

在你的php文件中

 if($result > 0 ){
 echo 1;
 } else {
 echo 0;
 }

现在你的函数在你的 ajax 调用中

function(data){
  if(data == 1){
        $('#message').html('<div id="alertFadeOut" style="color: green">Added to favorites!</div>'); // Diplay message with a fadeout
                      $('#alertFadeOut').fadeOut(3000, function () {
                          $('#alertFadeOut').text('');
                      }); 
    }
 }

【讨论】:

  • 这项工作适用于 ajax 部分.. 没有出现任何错误,正在保存到数据库中,但没有 #message
  • 检查您的消息 div 是否隐藏或获取任何 html
  • 是的,是的,我看到了......它就在那里。感谢您的帮助!
  • 乐于助人。 @JasonPaddle 标记我的答案也已验证,它可能对其他人有所帮助
  • 我会在 3 分钟内 ;)
猜你喜欢
  • 2016-12-27
  • 1970-01-01
  • 1970-01-01
  • 2015-04-26
  • 1970-01-01
  • 2016-10-09
  • 2021-08-17
  • 2023-03-17
  • 2023-04-06
相关资源
最近更新 更多