【问题标题】:How to handle check uncheck all checkboxes in ruby on rails如何在 ruby​​ on rails 中处理检查取消选中所有复选框
【发布时间】:2020-01-31 15:00:52
【问题描述】:

我是该领域的新手。我遇到了一个解决方案,例如我在表头中有复选框,通过该复选框,我必须选中取消选中所有子复选框。 我向父复选框添加了一个 onclick 事件,如下所示

%th End Date
%th= check_box_tag id: "select_all_checkboxes", label: "B2B Check", onclick:"Tickets.select_all_checks();"
%th SLA

这是表格数据中子复选框的位置。

- products.each_with_index do |p, index|
 - content_for :products_brief, flush: true do

%tr
 %td{ "type"=> "button", "class"=> "btn btn-link has-popover", "data-toggle"=> "popover", "title"=> "#{p.serial_no}", "data-content"=> "#{yield(:products_brief)}", "data-html" => "true", "data-trigger" => "hover", "data-placement" => "right" }
- if params[:page]
 = (index+1)+ 10*(params[:page].to_i-1)
- else
 = (index+1)
 %td=p.serial_no
- if params[:from_where] != "cus_product"
 %td= text_field_tag "contract_product_additional_params[#{p.id}][amount]",0, class: "form-control cus_product_amount1", required: true
 %td= text_field_tag "contract_product_additional_params[#{p.id}][discount_amount]",0, class: "form-control cus_product_disamount1", required: true
 %td= text_field_tag "contract_product_additional_params[#{p.id}][contract_start_at]",Date.today, class: "datepicker form-control", required: true
 %td= text_field_tag "contract_product_additional_params[#{p.id}][contract_end_at]", Date.today, class: "datepicker form-control", required: true
 %td= check_box_tag "contract_product_additional_params[#{p.id}][contract_b2b]",nil, class: "form-control selectable_checkbox", required: true
 %td= select_tag "contract_product_additional_params[#{p.id}][sla_id]", options_from_collection_for_select(SlaTime.where(active: 1), :id, :description ) , label_method: :description, include_blank: false, label: "SLA"
 %td= label_tag "#{p.owner_customer_name}"
 %td.hide= hidden_field_tag "contract_product_additional_params[#{p.id}][installed_location_id]", p.owner_customer_id
 %td= label_tag "#{p.location_address.try(:full_address)}"
 %td.hide= hidden_field_tag "contract_product_additional_params[#{p.id}][location_address_id]",p.location_address_id

我不知道要为这个事件编写 jquery 代码。你能帮帮我吗

select_all_checks: ->
    _this =$(e)

【问题讨论】:

    标签: jquery ruby ruby-on-rails-4 coffeescript


    【解决方案1】:

    JavaScript:

    $(document).ready(function(){
      $('#checkbox-selector').click(function(){
        if ($(this).prop("checked") == true) {
          $('#other-selector input:checkbox').prop('checked', true);
        }
      });
    });
    

    CoffeScript:

    $(document).ready ->
      $('#checkbox-selector').click ->
        if $(this).prop('checked') == true
          $('#other-selector input:checkbox').prop 'checked', true
        return
      return
    

    记得用你的选择器替换#checkbox-selector

    你可以看这里。 https://codepen.io/oksromn/pen/PoYMEWo

    要快速轻松地将 javascript 转换为 coffescript,请使用 http://js2.coffee

    【讨论】:

    • Um 试过这个,但出现Reserved word 的错误。嗯,使用 coffeescript lang。所以你能告诉我如何使用 coffeescript 更改代码吗?
    猜你喜欢
    • 2015-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    • 1970-01-01
    相关资源
    最近更新 更多