2013-03-15 48 views
0

当我按下其中一个按钮时,我有一组按钮。变化成为所有的类。但我希望它只在活跃的div上。只更改活动div而不是所有具有相同名称的类

我知道我应该使用$这个。但我无法管理它。有任何想法吗 ?

function close(){ 
     $('.close').click(function() { 
     $.ajax({ 
      url: "php/functions.php", 
      type: "POST", 
      data: "php", 
      success: function(html) { 
       var note = $(".note").html(html); 
      } 

     }); 

return false; 
}); 
} 

PHP文件只是包含回声

<?php echo "Heloo" ?> 

的HTML

<div class="note"> 
<div class="close"></div> 
</div> 
<div class="note"> 
<div class="close"></div> 
</div> 
+0

请提供一个小提琴 – honk31 2013-03-15 18:41:44

回答

1

你想要的是这个 -

$('.close').click(function() { 
    var $this = $(this); 
    $.ajax({ 
    url: "php/functions.php", 
    type: "POST", 
    data: "php", 
    success: function(html) { 
     $this.closest(".note").html(html); 
     //or 
     $this.parent().html(html); 
     //or 
     $this.parent().html(html); 
    } 
    }); 
}); 

然而,这将覆盖你的亲密股利。如果你想保持你的亲密格 -

HTML:

<div class="notewrap"> 
    <div class="note"></div> 
    <div class="close"></div> 
</div> 
<div class="notewrap"> 
    <div class="note"></div> 
    <div class="close"></div> 
</div> 

的Javascript(仅使用在回调所提供的选项之一):

$('.close').click(function() { 
    var $this = $(this); 
    $.ajax({ 
    url: "php/functions.php", 
    type: "POST", 
    data: "php", 
    success: function(html) { 
     //Option 1 - will work even if you change the html structure a fair bit 
     $this.parents(".notewrap").find(".note").html(html); 
     //Option 2 - will only work if your structure remains very similar 
     $this.siblings(".note").html(html); 
     //Option 3 
     $this.parent().find(".note").html(html); 
    } 
    }); 
}); 
+0

谢谢!这是一个很好的解决方案,其实我想要做的事情是删除整个可以吗? – Dymond 2013-03-15 19:34:27

+1

让你的回调$ this.parent()。remove(); – iabw 2013-03-15 19:42:52

+0

非常感谢你:) – Dymond 2013-03-15 19:48:34

0
$('.close').click(function() { 
    $(this).addClass("SOMETHING"); <--- like this 
     $.ajax({ 
      url: "php/functions.php", 
      type: "POST", 
      data: "php", 
      success: function(html) { 
       var note = $(".note").html(html); 
      } 

     }); 
+0

大家好!我仍然遇到同样的问题。 – Dymond 2013-03-15 18:58:38

0

如果我正确理解你的问题,你想只是改变了按钮那是在ajax success回调内点击的?这在回调中不起作用,因为它不会返回按钮(它会返回窗口,我认为,不确定)。
你必须在ajax文章之前获得对按钮的引用。试试这个:

$('.close').click(function() { 
    var self = $(this); 
    $.ajax({ 
     url: "php/functions.php", 
     type: "POST", 
     data: "php", 
     success: function(html) { 
      //Now you can do whatever you want with 'self', which is the button that was clicked 
      self.addClass('myclasstoadd'); 
      var note = $(".note").html(html); 
     } 
    }); 
+0

。试试这个:) brb – Dymond 2013-03-15 18:51:41

+0

我试过了,但是在div上不但获得了相同的结果,而且获得了相同的结果:( – Dymond 2013-03-15 18:58:08

+0

你能用html提供[jsFiddle](http://jsfiddle.net/)吗? – Marcus 2013-03-15 19:04:49

相关问题