隐藏已删除的元素(对象)的Ajax

huangapple go评论77阅读模式
英文:

Ajax hide deleted element(object)

问题

success:function(){}中,你可以使用以下代码来隐藏被删除的远程地址,而无需刷新页面:

success:function(){
    $('[data-address="' + _aId + '"]').closest('.address').hide();
}

这段代码会找到具有与点击按钮关联的data-address属性相匹配的最接近的包含地址的容器,然后将其隐藏起来。这将在单击按钮后隐藏相应的地址,而不需要刷新整个页面。

英文:

Learning Django and can't find solution. How hide deleted object on page.

html

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

{% extends &#39;base.html&#39; %}
{% load static %}
{% block content %}
&lt;main class=&quot;container my-4&quot;&gt;
	&lt;h3 class=&quot;my-4 border-bottom pb-1&quot;&gt;Адреса &lt;a href=&quot;{% url &#39;add_address&#39; %}&quot; class=&quot;float-right btn btn-sm btn-success&quot;&gt;&lt;i class=&quot;fa fa-plus&quot;&gt;  Add address&lt;/i&gt;&lt;/a&gt;&lt;/h3&gt;
	&lt;div class=&quot;row&quot;&gt;
		&lt;div class=&quot;col-md-3&quot;&gt;
			{% include &#39;user/user-sidebar.html&#39; %}
		&lt;/div&gt;
		&lt;div class=&quot;col-md-9&quot;&gt;
			&lt;div class=&quot;row&quot;&gt;
				{% for add in addbook %}
				&lt;div class=&quot;col-md-4&quot;&gt;
					&lt;div class=&quot;card mb-3 {% if add.status %}border-secondary shadow{% endif %} address{{add.id}} address&quot;&gt;
						&lt;div class=&quot;card-body&quot;&gt;
							&lt;p class=&quot;box-element&quot;&gt;{{add.address}}&lt;/p&gt;
							&lt;p class=&quot;card-text&quot;&gt;{{add.mobile}}&lt;/p&gt;
						&lt;/div&gt;
						{% if add.status %}
						&lt;div class=&quot;card-footer&quot;&gt;
							&lt;i class=&quot;fa fa-check-circle text-success check{{add.id}} check&quot;&gt;&lt;/i&gt;
							&lt;button data-address=&quot;{{add.id}}&quot; class=&quot;btn btn-sm btn-info activate-address btn{{add.id}} actbtn&quot; style=&quot;display:none;&quot; &gt;Activate&lt;/button&gt;
							&lt;button data-address=&quot;{{add.id}}&quot; class=&quot;btn btn-sm btn-danger delete-address&quot;&gt;&lt;i class=&quot;fa fa-trash&quot;&gt;&lt;/i&gt;&lt;/button&gt;
							&lt;a href=&quot;{% url &#39;update-address&#39; add.id %}&quot; class=&quot;float-right&quot;&gt;&lt;i class=&quot;fa fa-edit&quot;&gt;&lt;/i&gt;&lt;/a&gt;
						&lt;/div&gt;
						{% else %}
						&lt;div class=&quot;card-footer footer{{add.id}}&quot;&gt;
							&lt;i style=&quot;display:none;&quot; class=&quot;fa fa-check-circle text-success check{{add.id}} check&quot;&gt;&lt;/i&gt;
							&lt;button data-address=&quot;{{add.id}}&quot; class=&quot;btn btn-sm btn-info activate-address btn{{add.id}} actbtn&quot;&gt;Activate&lt;/button&gt;
							&lt;button data-address=&quot;{{add.id}}&quot; class=&quot;btn btn-sm btn-danger delete-address&quot;&gt;&lt;i class=&quot;fa fa-trash&quot;&gt;&lt;/i&gt;&lt;/button&gt;
							&lt;a href=&quot;{% url &#39;update-address&#39; add.id %}&quot; class=&quot;float-right&quot;&gt;&lt;i class=&quot;fa fa-edit&quot;&gt;&lt;/i&gt;&lt;/a&gt;
						&lt;/div&gt;
						{% endif %}
					&lt;/div&gt;
				&lt;/div&gt;
				{% endfor %}
			&lt;/div&gt;
		&lt;/div&gt;
	&lt;/div&gt;
&lt;/main&gt;
{% endblock %}

<!-- end snippet -->

js

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(document).on(&#39;click&#39;,&#39;.delete-address&#39;,function(){
    var _aId=$(this).attr(&#39;data-address&#39;);
    var _vm=$(this);
    // Ajax
    $.ajax({
        url:&#39;/delete-address&#39;,
        data:{
            &#39;id&#39;:_aId,
        },
        dataType:&#39;json&#39;,
        beforeSend:function (){
            _vm.attr(&#39;disabled&#39;, true)
        },
        success:function(){
            $(&quot;_aId&quot;).hide();
        }
    });
    // End
})

<!-- end snippet -->

What I must put in success:function(){} to make the remote address disappear after the button is clicked, without page refresh?

答案1

得分: 1

你可以使用 .closest() 来定位 .card.parent 来定位 .col-md-4,最后使用 .remove 从 DOM 中移除它。

将 JavaScript 部分更改为以下内容:

$(document).on('click','.delete-address',function(){
    var _aId=$(this).attr('data-address');
    var _vm=$(this).attr('disabled', true);
    // Ajax
    $.ajax({
        url:'/delete-address',
        data:{
            'id':_aId,
        },
        dataType:'json',
        success:function(){
            _vm.closest('.card').parent().remove();
        }
    });
})
英文:

You can use .closest() to target the .card, .parent to target the .col-md-4 and finally .remove to remove it from the DOM.

Change the javascript part to something like this

$(document).on(&#39;click&#39;,&#39;.delete-address&#39;,function(){
    var _aId=$(this).attr(&#39;data-address&#39;);
    var _vm=$(this).attr(&#39;disabled&#39;, true);
    // Ajax
    $.ajax({
        url:&#39;/delete-address&#39;,
        data:{
            &#39;id&#39;:_aId,
        },
        dataType:&#39;json&#39;,
        success:function(){
            _vm.closest(&#39;.card&#39;).parent().remove();
        }
    });
})

答案2

得分: 0

.remove()会起作用。您必须选择正确的DOM元素。

success:function(){
    $("_aId").remove();
}
英文:

.remove() will work. You have to select the right dom element.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  success:function(){
                $(&quot;_aId&quot;).remove();
            }

<!-- end snippet -->

答案3

得分: 0

你需要修改它

beforeSend:function (){
   //_vm.attr('disabled', true)
   $(_vm).attr('disabled', true)
},

因为"_vm"不对应于一个jQuery对象

英文:

you need modify it

beforeSend:function (){
   //_vm.attr(&#39;disabled&#39;, true)
   $(_vm).attr(&#39;disabled&#39;, true)
},

Because "_vm" does not correspond to a jquery object

huangapple
  • 本文由 发表于 2023年2月27日 15:00:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/75577531.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定