Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ajax 仅更改活动div,而不是更改具有相同名称的所有类_Ajax_Jquery - Fatal编程技术网

Ajax 仅更改活动div,而不是更改具有相同名称的所有类

Ajax 仅更改活动div,而不是更改具有相同名称的所有类,ajax,jquery,Ajax,Jquery,当我按下其中一个按钮时,我有一组带有按钮的类。所有班级都发生了变化。但我希望它只在活动分区上 我知道我应该用这个。但我没办法应付。有什么想法吗 function close(){ $('.close').click(function() { $.ajax({ url: "php/functions.php", type: "POST", data: "php", succe

当我按下其中一个按钮时,我有一组带有按钮的类。所有班级都发生了变化。但我希望它只在活动分区上

我知道我应该用这个。但我没办法应付。有什么想法吗

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>

$('.close')。单击(函数(){

$(this).addClass(“SOMETHING”);如果我正确理解您的问题,您只是想更改在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);
        }
    });
你想要的是这个-

$('.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);
    }
  });
});
但是,这将覆盖关闭div。如果要保留关闭div-

HTML:


请提供一个小提琴。将尝试此:)brbI已尝试,但在两个div上得到相同的结果,而不仅仅是在一个:(我会尝试,因为它包括一个php brb.Hey。让troubel知道php和JSFIDLE。能给你项目私有的链接吗?嗨,我仍然遇到同样的问题。谢谢!这是一个很好的解决方案,实际上我想做的事情是移除整个框。有可能吗?让你的回调$this.parent().remove();
$('.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);
    }
  });
});
<div class="notewrap">
  <div class="note"></div>
  <div class="close"></div>
</div>
<div class="notewrap">
  <div class="note"></div>
  <div class="close"></div>
</div>
$('.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);
    }
  });
});