【问题标题】:Hide dynamically created button by clicking on it通过单击隐藏动态创建的按钮
【发布时间】:2016-03-01 20:52:49
【问题描述】:

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

我想要什么:

  1. 点击“添加”
  2. 结果已添加到页面上的 div 中
  3. “添加”按钮消失

工作 p. 1-2 但不是 3

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()
        ))

【问题讨论】:

    标签: jquery ajax django coffeescript django-1.8


    【解决方案1】:

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

    $(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()
        ))
    

    【讨论】:

    • 这解决了问题。直觉上我得到了我错的地方,但是'this inside get'指的是什么对象?
    • 好问题。一些实验表明它是jQuery Ajax settings object
    猜你喜欢
    • 2014-11-04
    • 1970-01-01
    • 2020-10-04
    • 2018-06-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-27
    相关资源
    最近更新 更多