【发布时间】:2016-06-20 23:08:26
【问题描述】:
我正在尝试从下面的代码中调用函数“remove_category”,但它返回“未捕获的 ReferenceError: remove_category is not defined”
$('.category-checkbox').on 'click', ->
category_id = $(this).attr('id')
$(this).toggleClass 'selected'
if $(this).hasClass('selected')
save_category category_id
else
remove_category category_id
return
删除类别
window.remove_category = (remove_category) ->
alert 'called'
sessionStorage.removeItem 'categories', remove_category
stored_categories = jQuery.grep(stored_categories, (value) ->
value != remove_category
)
console.log stored_categories
sessionStorage.setItem 'categories', JSON.stringify(stored_categories)
return
我不确定如何解决此问题。谁能帮帮我?
谢谢!
【问题讨论】:
-
而且
window.remove_category = ...肯定会在 onclick 处理程序执行之前执行...?!显式创建全局函数/属性/变量 BTW 不是一个好主意... -
我不太确定“窗口”的作用。有没有其他方法可以解决这个问题?
-
只需在与 onclick 处理程序相同的范围内创建函数...!?
remove_category = ...; $(..).on 'click', -> ...;