首页 > 解决方案 > django 管理列表自定义按钮的点击事件

问题描述

class TestAdmin(admin.ModelAdmin):
    class Media:
        js = (
            'js/common.js',
        )

    list_display = ['custom_actions']

    def custom_actions(self, obj):
        return mark_safe(
            '<a class="button call_alert" href="#" data-msg="A">A</a>&nbsp;'
            '<a class="button call_alert" href="#" data-msg="B">B</a>'
        )
    custom_actions.short_description = 'Custom Actions'

admin.site.register(Test, TestAdmin)
(function($) {
    $(".call_alert").on("click", function() {
        alert($(this).data("msg"));  // ★★★ Dose not!!! ★★★
    });
})($);

错误信息 :

Uncaught TypeError: $ is not a function at common.js:2 at common.js:5

我怎样才能收到警报消息?

这是不可能的吗?

请帮忙..

标签: javascriptdjangodjango-admincustom-action

解决方案


最后,我明白了。:)

脚本更改如下。

if(!$) $ = django.jQuery;
$(function(){
    $(".call_alert").on("click", function() {
        alert($(this).data("msg"));
    });
});

感谢你所做的一切。:D


推荐阅读