jquery中元素的id不会改变?

发布于 2024-10-09 09:46:57 字数 564 浏览 0 评论 0原文

$("#savenew").live('click', function(e)  {
               var user=<?php echo $user?>;
                 $.ajax({
                type: "POST",
                url: "actions/sub.php",
                data:{user: user} ,
                success: function(){
                  $('#savenew').html('<span>Unsubscribe</span>');
    $(this).attr('id', 'clean'); // this my problem my problem
                        }
            });
     }); 

ajax请求后的id,没有从savenew更改为clean,但html完全没问题,所以我知道ajax请求正在工作。你认为问题是什么?

i.e.

$("#savenew").live('click', function(e)  {
               var user=<?php echo $user?>;
                 $.ajax({
                type: "POST",
                url: "actions/sub.php",
                data:{user: user} ,
                success: function(){
                  $('#savenew').html('<span>Unsubscribe</span>');
    $(this).attr('id', 'clean'); // this my problem my problem
                        }
            });
     }); 

the id after the ajax request, is not changing from savenew to clean, but the html is perfectly fine, so i know the ajax request is working. what do u thiunk the problem is?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浮生面具三千个 2024-10-16 09:46:57

您只需要保存 context (通过 context $.ajax() 的选项),因此 this 仍然引用 #savenew,如下所示:

$("#savenew").live('click', function(e)  {
  var user=<?php echo $user?>;
  $.ajax({
    context: this, //add this!
    type: "POST",
    url: "actions/sub.php",
    data:{user: user} ,
    success: function(){
      $(this).attr('id', 'clean').html('<span>Unsubscribe</span>');
    }
  });
}); 

另请注意,这允许你把事情链起来并清理一下。

You just need to save the context (via the context option for $.ajax()) so this still refers to #savenew, like this:

$("#savenew").live('click', function(e)  {
  var user=<?php echo $user?>;
  $.ajax({
    context: this, //add this!
    type: "POST",
    url: "actions/sub.php",
    data:{user: user} ,
    success: function(){
      $(this).attr('id', 'clean').html('<span>Unsubscribe</span>');
    }
  });
}); 

Also note that this allows you to chain and clean things up a bit.

热情消退 2024-10-16 09:46:57

success: function(){ 中的 $(this) 不引用 $('#savenew')

正如您在上面一行中所做的那样,您需要通过 id 引用它:

$('#savenew').attr('id', 'clean');

$(this) inside success: function(){ does not refer to $('#savenew').

As you do in the line above, you need to reference it by id:

$('#savenew').attr('id', 'clean');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文