Javascript Coffeescript jQuery每个循环
我对Coffeescript比较陌生,我遇到了一些我认为与瘦箭vs胖箭有关的问题 我有以下类和构造函数:Javascript Coffeescript jQuery每个循环,javascript,jquery,coffeescript,Javascript,Jquery,Coffeescript,我对Coffeescript比较陌生,我遇到了一些我认为与瘦箭vs胖箭有关的问题 我有以下类和构造函数: class @scenarioEditPage constructor: -> @getParentAttributes() 并且@getParentAttributes()是 本质上,我试图做的是循环遍历表单中的所有隐藏字段,并将它们的值分配给属性散列中的键/值对,以便以后使用。示例:带有id=some\u id和value=some\u value的隐藏输入字段变为p
class @scenarioEditPage
constructor: ->
@getParentAttributes()
并且@getParentAttributes()是
本质上,我试图做的是循环遍历表单中的所有隐藏字段,并将它们的值分配给属性散列中的键/值对,以便以后使用。示例:带有id=some\u id
和value=some\u value
的隐藏输入字段变为properties[some\u id]=some\u value
任何帮助都将不胜感激。谢谢大家! 首先,我不会使用
class@scenariosEditPage
,除非你真的需要<代码>类场景编辑页面会更好
试着这样做:
jQuery的每个
方法都会更改内部代码的上下文,因此基本上您必须使用双箭头来保留上下文,或者使用带有额外变量的简单箭头,例如ctx=@
,检查以下内容:
# We have some method @doAnything, so :
@doAnything
# isnt undefined...
# Now:
# this works
$('selector...').each (index, elem) =>
# @ == this == caller's context
@doAnything $(elem)
# or this works fine
ctx = @
$('selector...').each () ->
# this == DOM Element
# ctx == caller's context
# ! ctx isnt @ !
ctx.doAnything $(this)
# this doesnt work
$('selector...').each () =>
# @ == DOM Element, so @doAnything is undefined
@doAnything $(this)
您的代码中有一个小错误:
@properties[$(element).attr('id')] = element.val()
element.val()未定义,请按如下方式修复:
@properties[$(element).attr('id')] = $(element).val()
或者更好:
$element = $ element
@properties[$element.attr('id')] = $element.val()
@properties[$(element).attr('id')] = $(element).val()
$element = $ element
@properties[$element.attr('id')] = $element.val()