Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在页面加载时触发jQuery一次_Javascript_Jquery_Ruby On Rails 3_Coffeescript - Fatal编程技术网

Javascript 在页面加载时触发jQuery一次

Javascript 在页面加载时触发jQuery一次,javascript,jquery,ruby-on-rails-3,coffeescript,Javascript,Jquery,Ruby On Rails 3,Coffeescript,更新8/18/12:我有没有办法让下面的问题更容易回答 我在js.coffee文件中有以下代码,它可以用于向表单中添加行。我想让它自动添加加载的第一行,但不知道如何让它这样做 $('form').on 'click', '.add_fields', (event) -> time = new Date().getTime() regexp = new RegExp($(this).data('id'), 'g') $(this).before($(this).data('fields

更新8/18/12:我有没有办法让下面的问题更容易回答

我在js.coffee文件中有以下代码,它可以用于向表单中添加行。我想让它自动添加加载的第一行,但不知道如何让它这样做

$('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()
(如果这看起来很熟悉,它是从中逐字盗取的)

更新:我尝试了@Baldrick的建议,但仍然没有成功:

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()
更新2:这里有更多信息:

我的html(重要部分):


我本想避免把这个问题写成小说,但事实是。。。我希望有足够的信息让它工作。

加载页面时将执行以下代码:

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

如果代码应该只在一个页面上执行,请将其放入仅由该页面调用的javascript文件中,或者在方法中添加一个测试,以便仅在需要时执行代码。

您所说的“无骰子”是什么意思?控制台中有错误吗?什么都没发生?另外,您希望
$(document).ready'.receive form',(event)->
做什么?是的,控制台什么也没给我。我有一个内置于rails中的表单,当我单击带有
class=“add_fields”
的按钮时,我会得到一个新的表单副本。这是一个嵌套的模型表单。我可以把它贴在这里,但我认为没有必要。我似乎无法将表单与页面一起加载。我可以获取要加载的顶级模型字段,但不能加载嵌套的字段。。。我希望让jQuery加载它们。
<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>
<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>
  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
$(document).ready(function () {
  // put here code to execute on page loading
});