【问题标题】:Hide div from ajax从ajax隐藏div
【发布时间】:2021-04-12 06:19:33
【问题描述】:

我已经创建了一个 AdminCP 来管理我的所有食谱、用户、详细信息等...所以,我在这里运行这段代码,将我的食谱从共享变为草稿。一切都很好,它正常工作,但我想隐藏它包含我点击的按钮的 TD。

     $(document).on("click", "[data-action]", function(e){
        e.preventDefault();
        
        if(window.confirm("Are you sure to draft this Article?")){
            var draft_id = $(this).attr("data-id");
            var action = $(this).attr("data-action");
           
            $.ajax({
                url: "actions-recipe",
                type: "post",
                data: {
                    draft_id:draft_id,
                    action:action
                },
                success: function(data){
                    // I would like to hide the td that it contains this button
                }
            });
        }else{
            return false;
        }
    });

HTML:

                 <td class="t-c dropdown">
                    <span class="dropbtn"><i class="fa fa-ellipsis-v" aria-hidden="true"></i></span>
                    <div class="drop-content">
                    <a id="edit" href="modify-news?nid='.$rows["id"].'" class="icon">Edit</a>
                    <a id="draft" role="button" class="icon" data-id="'.$rows["id"].'" data-action="drafted">Draft</a>
                    <a id="delete" role="button" class="icon" data-id="'.$rows["id"].'" data-action="deleted">Delete</a>
                    </div>
                </td>

我能做什么?任何想法?感谢大家! :)

【问题讨论】:

    标签: javascript php html json ajax


    【解决方案1】:

    js:

    document.querySelector('td.t-c.dropdown').classList.add('hidden');
    

    或者jquery

    $('td.t-c.dropdown').addClass('hidden');
    

    css:

    td.t-c.dropdown.hidden {
        display:none;
    }
    

    【讨论】:

    • 您的意思是写:.hidden { display:none; }
    【解决方案2】:

    您需要在成功操作中添加以下代码。

    success: function(data){
         $('a[data-action = '+action+']').closest('td.dropdown').hide();
    }
    

    【讨论】:

    • 嗨,我之前已经尝试过了,但这不是我想要的。这隐藏了我单击的按钮,而不是它包含按钮的 div ..
    • 我根据您的要求更新了答案。
    • 我怎样才能在 $(document).ready(function(){ 之后运行它而不隐藏它们包含类下拉列表的所有 div,而只隐藏我通过按钮选择的 div?
    • 好的,我已经用 cicle for and var i 修复了。 :P
    【解决方案3】:

    给 td 标签提供 ID 并用 jquery 隐藏

    解决办法

    HTML

    <td id="myid" class="t-c dropdown">
      <span class="dropbtn"><i class="fa fa-ellipsis-v" aria-hidden="true"></i></span>
      <div class="drop-content">
        <a id="edit" href="modify-news?nid='.$rows["id"].'" class="icon">Edit</a>
        <a id="draft" role="button" class="icon" data-id="'.$rows["id"].'" data-action="drafted">Draft</a>
        <a id="delete" role="button" class="icon" data-id="'.$rows["id"].'" data-action="deleted">Delete</a>
      </div>
    </td>
    

    JAVASCRIPT

    $(document).on("click", "[data-action]", function(e){
      e.preventDefault();
    
      if(window.confirm("Are you sure to draft this Article?")){
        var draft_id = $(this).attr("data-id");
        var action = $(this).attr("data-action");
    
        $.ajax({
          url: "actions-recipe",
          type: "post",
          data: {
              draft_id:draft_id,
              action:action
          },
          success: function(data){
              $("#myid").hide()
          }
        });
      }else{
        return false;
      }
    });
    

    【讨论】:

    • 您好,感谢您的帮助!是的,我已经做到了,但我只有一个问题:因为我只有一个 div 作为 td,如果我有多个 td 会发生什么?我将隐藏所有它们还是仅隐藏它包含我单击的按钮的 td?
    • @VittoAlloggio 我很高兴它有帮助。要回答这个问题,只有得到“myid”的 td 才会隐藏。如果您不提供相同的 id,另一个 td 标签将不会隐藏
    • 感谢@jun 的帮助。我已经用多个 div 测试了这段代码,它只隐藏了包含我单击的按钮的 div。我祝福你有个美好的一天! :)
    猜你喜欢
    • 2014-05-22
    • 2012-09-15
    • 2020-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多