简体   繁体   中英

Fire jQuery once on page load

UPDATE 8/18/12: Is there any way I can make the following question easier to answer?

I have the following code in a js.coffee file and it works for adding lined to a form. I want it to automatically add the first line on load though and don't know how to get it to to do that...

$('form').on 'click', '.add_fields', (event) ->
 time = new Date().getTime()
 regexp = new RegExp($(this).data('id'), 'g')
 $(this).before($(this).data('fields').replace(regexp, time))
 event.preventDefault()

(If this looks familiar, it's stolen verbatim from Ryan Bates' Railscast #196 revised )

UPDATE: I tried @Baldrick's advice with the following and still no dice:

jQuery ->
  $('form').on 'click', '.add_fields', (event) ->
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
    event.preventDefault()

$(document).ready '.receive-form', (event) ->
    time = new Date().getTime()
    regexp = new RegExp($(this).data('id'), 'g')
    $(this).before($(this).data('fields').replace(regexp, time))
    event.preventDefault()

UPDATE 2: Here's some more info:

My html(the important parts):

<div class="modal-body ">
<%= @company.haves.new.warehouse_transactions.new %>
<%= form_for @company do |f| %>

  <div class="form-inline receive-form">
    <h4>Part Details:</h4>
  <%= f.fields_for :haves do |builder| %>
      <%#= render 'have_fields', f: builder %>
    <% end %>
  <%= link_to_add_fields "+", f, :haves, :warehouse_transactions %>
  </div>
</div>

have_fields:

<fieldset>
<%= f.text_field :product_title, :class => 'input-small text_field', :placeholder => "Product Title" %>
<%= f.fields_for :warehouse_transactions do |builder| %>
<%= render 'wht_fields', :f => builder %>
<% end %>
<%= f.hidden_field :_destroy %>
<%= link_to "-", '#', class: "remove_fields" %>
</fieldset>

wht_fields:

<fielset>
  <%= f.number_field :quantity, :class => 'input-small number_field', :placeholder => "Quantity" %>
  <%= f.text_field :cost, :class => 'input-small text_field', :placeholder => "Cost" %>
  <%= f.text_field :location, :class => 'input-small text_field', :placeholder => "Location" %>
  <%= f.text_field :batch, :class => 'input-small text_field', :placeholder => "Batch" %>
  <%= f.select :condition, ['Condition'] + WarehouseTransaction::CONDITIONS %>
  <%= f.hidden_field :_destroy %>
  <%= link_to "remove", '#', class: "remove_fields" %>
</fieldset>

here also is the rails helper associated with the link_to_add_fields:

  def link_to_add_fields(name, f, association, child_association = nil)
    new_object = f.object.send(association).klass.new
    id = new_object.object_id
    new_object.send(child_association).new if child_association
    fields = f.fields_for(association, new_object, child_index: id) do |builder|
      render(association.to_s.singularize + "_fields", f: builder)
    end
    link_to(name, '#', class: "add_fields", data: {id: id, fields: fields.gsub("\n", "")})
  end

I was hoping to avoid turning this question into a novel, but there it is... I hope that's enough info to get this thing working.

The code below will be executed when the page is loaded:

$(document).ready(function () {
  // put here code to execute on page loading
});

If the code should be executed only on one page, put it in a javascript file that is called only by this page, or add a test in the method to execute the code only when needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM