函数内的Coffeescript函数未在点击时调用

时间:2015-04-12 01:48:18

标签: javascript jquery function coffeescript

我确信这很简单,但我无法在click事件中调用此函数。我尝试了各种各样的化身,但是不能很好地发挥它。如果我在每次点击事件中移动if / else循环,它的工作没有问题。我想通过将循环移动到单个参考函数来使代码变得有点干。这是语法问题还是逻辑问题? - 提前致谢。

不工作

$(".filter_option").click ->
      $('#submission_list').children(".item ").show()
      checkIfItemsEmpty()

$('#current_filters').on 'click', 'a.filter_remove', ->
      $('#submission_list').children(".item").hide()
      checkIfItemsEmpty()

checkIfItemsEmpty = () ->   
  if !$('.item').filter(':visible').length
      $('#no_results').show()
      console.log('The show command was triggered')
  else
      $('#no_results').hide()
      console.log('The hide command was triggered')

WORKS

$(".filter_option").click ->
      $('#submission_list').children(".item ").show()
      if !$('.item').filter(':visible').length
         $('#no_results').show()
         console.log('The show command was triggered')
      else
         $('#no_results').hide()
         console.log('The hide command was triggered')

$('#current_filters').on 'click', 'a.filter_remove', ->
      $('#submission_list').children(".item").hide()
      if !$('.item').filter(':visible').length
         $('#no_results').show()
         console.log('The show command was triggered')
      else
         $('#no_results').hide()
         console.log('The hide command was triggered')

1 个答案:

答案 0 :(得分:1)

这是一个简单的格式化案例,我在发布后两秒就意识到了这一点!

似乎

checkIfItemsEmpty = () ->   
  if !$('.item').filter(':visible').length
      $('#no_results').show()
      console.log('The show command was triggered')
  else
      $('#no_results').hide()
      console.log('The hide command was triggered')

必须超越jQuery - >上班。

...所以

jQuery ->
   $(".filter_option").click ->
      $('#submission_list').children(".item ").show()
      checkIfItemsEmpty()

   $('#current_filters').on 'click', 'a.filter_remove', ->
      $('#submission_list').children(".item").hide()
      checkIfItemsEmpty()

checkIfItemsEmpty = () ->   
  if !$('.item').filter(':visible').length
      $('#no_results').show()
      console.log('The show command was triggered')
  else
      $('#no_results').hide()
      console.log('The hide command was triggered')

现在就像梦一样。