复选框按钮仅适用于第一次

时间:2012-08-23 08:00:05

标签: ruby-on-rails coffeescript

我有一个用gem制作的iphonestyle复选框按钮(ios-checkboxes)。我希望通过此按钮仅显示一些表行(已完成/未完成的规范),具体取决于按钮状态。如果我第一次按它,工作正确,之后它停止工作。

这是视图代码。

= 'Show completed specifications'
= check_box @specifications, :status_set
%br
%table(id = "specifications")
  %thead
    %tr
      %th Title
      %th Added
      %th
  %tbody
    - @specifications.each do |spec|
      - if spec.status
        %tr{class: "completed"} // here i add a class on the COMPLETED specifications
          %th= spec.title
          %th= spec.created_at.strftime("%d-%m-%y %H:%M")
          %th= link_to "View", project_specification_path(params[:project_id], spec.id)
      - else
        %tr{class: "not_completed"} // NOT COMPLETED specifications
          %th= spec.title
          %th= spec.created_at.strftime("%d-%m-%y %H:%M")
          %th= link_to "View", project_specification_path(params[:project_id], spec.id)

coffeescript代码

$('.iPhoneCheckContainer').live "click", -> // the checkbox class
  is_completed = @.checked
  if is_completed
    $('.completed').show()
    $('.not_completed').hide()
  else
    $('.completed').hide()
    $('.not_completed').show()

2 个答案:

答案 0 :(得分:1)

我怀疑你真正需要做的是:

if $(this).is(':checked')
  // Stuff when checked
else
  // stuff when not checked

这可能是检查普通复选框的理想方式。虽然我不确定ios复选框会产生什么样的标记,所以这可能适用于您,也可能不适合您。

编辑:

jquery中不推荐使用live方法。您应该使用on来绑定事件而不是bindlive

$('.iPhoneCheckContainer').on "click", ->
  // Event

答案 1 :(得分:0)

问题在于

is_completed = @.checked

看起来ios-checkboxes gem不像普通的复选框那样工作。

我通过在按钮上添加一个新类来解决问题,该函数可以在任何单击事件上将变量切换为true / false。根据该变量,我显示已完成/未完成的规范。

相关问题