简体   繁体   English

$(this).nextAll(“#…”)。eq(0).text(“ ***”)不起作用

[英]$(this).nextAll(“#…”).eq(0).text(“***”) is not working

I have problem with $(this).nextAll("#status").eq(0).text("Deleted") . 我对$(this).nextAll("#status").eq(0).text("Deleted") I want to in the tag <span> put a text: "Deleted", but but does not insert it... See the my code: 我想在标签<span>放置一个文本:“已删除”,但不插入它...请参阅我的代码:

admin.php PHP: admin.php PHP:

$sql = "SELECT * FROM `upload_img`";
        $result = mysqli_query($db, $sql);
        while ($row = mysqli_fetch_assoc($result)) {
            echo "<div class='image' data-id='".$row['id']."'><br>";
            echo "<span><strong>Image title</strong>: ".$row['image_title']."</span><br>";
            echo "<span><strong>User of image</strong>: ".$row['user_name']."</span><br>";
            echo "<span><strong>Image file name</strong>: ".$row['image']."</span><br>";
            echo "<span id='status'></span>";
            echo "<button class='delete' data-id='".$row['id']."'>Delete</button><hr>";
            echo "</div>";
        }

admin.js JS: admin.js JS:

$(".delete").on("click", function() {
var id = $(this).data("id");
$(this).nextAll("#status").eq(0).text("Deleted"); //This is not work!
$.post("adminServer.php", { id: id }, function(data) {
    data = JSON.parse(data);
    alert(data);
});

}); });

I do not know why it does not work... Thanks! 我不知道为什么它不起作用...谢谢!

Firstly part of your problem is that id attributes must be unique within the DOM. 首先,您的问题的一部分是id属性在DOM中必须唯一。 As such, only the first one will be recognised. 这样,仅第一个将被识别。 You need to change the HTML generated to use a class on the status element instead: 您需要更改生成的HTML,以改为在status元素上使用类:

$sql = "SELECT * FROM `upload_img`";
$result = mysqli_query($db, $sql);
while ($row = mysqli_fetch_assoc($result)) {
  echo '<div class="image" data-id="'.$row['id'].'"><br>';
  echo '<span><strong>Image title</strong>: '.$row['image_title'].'</span><br>';
  echo '<span><strong>User of image</strong>: '.$row['user_name'].'</span><br>';
  echo '<span><strong>Image file name</strong>: '.$row['image'].'</span><br>';
  echo '<span class="status"></span>'; // change the id to class here
  echo '<button class="delete" data-id="'.$row['id'].'">Delete</button><hr>';
  echo '</div>';
}

Secondly your DOM traversal logic isn't quite right. 其次,您的DOM遍历逻辑不太正确。 nextAll() goes through successive siblings, yet the status you're looking for is a preceding sibling. nextAll()经历连续的同级,但是您要查找的status是先前的同级。 You can either use prev() or get the closest() parent, then find() within that. 您可以使用prev()或获取closest()父对象,然后在其中find() The latter is far more robust should you ever change the order of HTML elements. 如果您曾经更改HTML元素的顺序,则后者要强大得多。 Also note that eq(0) is redundant as there's only one status generated per .image container. 还要注意,由于每个.image容器仅生成一个status ,因此eq(0)是多余的。

$(".delete").on("click", function() {
  var id = $(this).data("id");
  $(this).closest('div.image').find('.status').text("Deleted");

  $.post("adminServer.php", { id: id }, function(data) {
    data = JSON.parse(data);
    console.log(data); // don't use alert() for debugging.
  });
});

Check below code with sample data. 检查下面的代码与示例数据。 Hope it helps. 希望能帮助到你。

$(this).siblings(".status").eq(0).text("Deleted");

It gets nearby sibling with id status and update with status "Deleted" 它以ID状态进入附近的同级状态,并以状态“已删除”进行更新

 $(".delete").on("click", function() { var id = $(this).data("id"); $(this).siblings(".status").eq(0).text("Deleted"); /* $.post("adminServer.php", { id: id }, function(data) { data = JSON.parse(data); console.log(data); // don't use alert() for debugging. }); */ }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class='image' data-id='1'> <br> <span><strong>Image title</strong>: Test1</span><br> <span><strong>User of image</strong>: Test1</span><br> <span><strong>Image file name</strong>: Test1</span><br> <span class='status'></span> <button class='delete' data-id='1'>Delete</button><hr> </div> <div class='image' data-id='2'> <br> <span><strong>Image title</strong>: Test2</span><br> <span><strong>User of image</strong>: Test2</span><br> <span><strong>Image file name</strong>: Test2</span><br> <span class='status'></span> <button class='delete' data-id='2'>Delete</button><hr> </div> <div class='image' data-id='3'> <br> <span><strong>Image title</strong>: Test3</span><br> <span><strong>User of image</strong>: Test3</span><br> <span><strong>Image file name</strong>: Test3</span><br> <span class='status'></span> <button class='delete' data-id='3'>Delete</button><hr> </div> 

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

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