【问题标题】:AJAX delete request + remove li by idAJAX删除请求+按id删除li
【发布时间】:2019-06-17 17:25:24
【问题描述】:

我想通过单击其中的“x”按钮来删除列表项。当我单击“x”按钮时,它不会删除它。 AJAX 响应为“OK”,但我的所有列表项仍然存在。

我在成功方法中使用 .remove() 尝试了不同的选项;我尝试创建一个单独的删除函数以在 AJAX 承诺解决时调用;我尝试在 AJAX 请求中添加/删除键/值对;我对 AJAX 请求和服务器端操作还很陌生,但是文档和视频并没有帮助我解决这个问题。

const getAllChirps = () => {
    $.ajax({
        type: "GET",
        url: "/api/chirps/"
    })
        .then(chirps => {
            $('ul').empty();
            chirps.forEach(chirp => {
                $('ul').append(`<li id=${chirp.id} class="list-group-item"> 
            ${chirp.text} 
            <button onclick="deleteChirp()"> X </button>
            </li>`)
            })
        })
};
const deleteChirp = () => {
    let id = $('li').attr('id')
    alert('Delete button works');
    $.ajax({
        type: "DELETE",
        url: '/api/chirps/:' + id,
        success: function () {
            $('li').remove(id);
            getAllChirps();
        }
    }).then(res => console.log(res));
}

我希望在单击“x”按钮时删除列表项。相反,我在控制台中收到“OK”响应,并且没有删除任何列表项。

【问题讨论】:

    标签: jquery node.js ajax


    【解决方案1】:

    将按钮作为参数传递给 deleteChirp 函数。然后找到父li如下demo。

    const getAllChirps = () => {
        $('ul').empty();
    	for(var i=0; i<3; ++i)
    		$('ul').append('<li id="A' + i + '" class="list-group-item">A' + i + ' Label<button onclick="deleteChirp(this)"> X </button></li>');
    };
    
    const deleteChirp = (ele) => {
        $(ele).closest("li").remove();
    };
    
    $(document).ready(function() {
        getAllChirps();
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <ul><li>Test</li></ul>

    【讨论】:

      【解决方案2】:

      问题是deleteChirp 不知道li 应该在let id = $('li').attr('id') 中引用什么

      您是否尝试将 id 添加到点击功能?

      onclick="deleteChirp('+chirp.id+')"
      

      然后你可以像这样在函数中获取它:

      const deleteChirp = (liId) => {
      

      完整代码:

      const getAllChirps = () => {
          $.ajax({
              type: "GET",
              url: "/api/chirps/"
          })
              .then(chirps => {
                  $('ul').empty();
                  chirps.forEach(chirp => {
                      $('ul').append(`<li id=${chirp.id} class="list-group-item"> 
                  ${chirp.text} 
                  <button onclick="deleteChirp('+chirp.id+')"> X </button>
                  </li>`)
                  })
              })
      };
      const deleteChirp = (liId) => {
          let id = liId
          alert('Delete button works');
          $.ajax({
              type: "DELETE",
              url: '/api/chirps/:' + id,
              success: function () {
                  $('li#'+id).remove();
                  getAllChirps();
              }
          }).then(res => console.log(res));
      }
      

      【讨论】:

      • 不幸的是,这并没有解决问题;我得到相同的结果(li 没有删除)。
      • 生成 html 后尝试检查按钮,看看它是否在 deleteChirp() 内有一个 id,它也应该是 $('li#' + id).remove();
      猜你喜欢
      • 1970-01-01
      • 2016-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-02-05
      • 2015-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多