通过单击隐藏动态创建的按钮

时间:2022-01-08 20:19:21

In my Django project I have a ajax search which returns a list of results along with button "Add"

在我的Django项目中,我有一个ajax搜索,它返回一个结果列表以及按钮“Add”

What I want:

我想要的是:

  1. Click "Add"
  2. 点击“添加”
  3. Result is added to div on page
  4. 结果将添加到页面上的div
  5. Button "Add" disappears
  6. 按钮“添加”消失

Work p. 1-2 but not 3

工作p。 1-2但不是3

jquery-ajax

jQuery的AJAX

$(document).on('click', '.button-add', ->
    catid = $(this).attr("data-catid")
    title = $(this).attr("data-title")
    url = $(this).attr("data-url")
    $.get('/test/auto_add_page/', {category_id:catid, title:title, url:url}, (data) ->
        $('#pages').html(data)
        $(this).hide()
        ))

1 个解决方案

#1


1  

this inside the get now refers to a different object. Save it in a variable in the outer block and refer to that.

现在get里面的这个指的是一个不同的对象。将其保存在外部块中的变量中并引用它。

$(document).on('click', '.button-add', ->
    button = $(this)
    catid = button.attr("data-catid")
    title = button.attr("data-title")
    url = button.attr("data-url")
    $.get('/test/auto_add_page/', {category_id:catid, title:title, url:url}, (data) ->
        $('#pages').html(data)
        button.hide()
    ))

#1


1  

this inside the get now refers to a different object. Save it in a variable in the outer block and refer to that.

现在get里面的这个指的是一个不同的对象。将其保存在外部块中的变量中并引用它。

$(document).on('click', '.button-add', ->
    button = $(this)
    catid = button.attr("data-catid")
    title = button.attr("data-title")
    url = button.attr("data-url")
    $.get('/test/auto_add_page/', {category_id:catid, title:title, url:url}, (data) ->
        $('#pages').html(data)
        button.hide()
    ))