如何使用数据属性将动态生成的id从rails应用程序传递给jQuery?尝试时,该值将返回未定义的值,除非我将其发送到警报框。
此代码(将调试链接用作黑客):
$(document).on "page:change", ->
$('.debug_link_id').click ->
alert $(this).data('dynamic_id)
在“警报”框中显示dynamic_id。
但是,使用相同的选择器,此代码:
$(document).on "page:change", ->
jqVar = $(this).data('dynamic_id')
$('.debug_link_id').click ->
alert jqVar
在警报框中显示“未定义”。使用.attr('data-dynamic_id')给出了相同的结果
请告诉我:
发布于 2016-04-10 19:18:44
尝试使用事件委托在处理程序中将this
设置为"#debug_link_id"
。
$(document).on "page:change", "#debug_link_id", ->
发布于 2016-04-10 19:19:15
那是因为你在错误的地方指的是this
。
$(document).on "page:change", ->
$('#debug_link_id').click ->
alert $(this).data('dynamic_id)// <-- That "this" is referring a object with id debug_link_id
$(document).on "page:change", ->
jqVar = $(this).data('dynamic_id') //<-- That "this" is referring a object that I don't know where this is come from...
$('#link_id').click ->
alert jqVar
https://stackoverflow.com/questions/36534314
复制相似问题