0

我在 rails 3 中使用 nested_form 并且我有一个用户可以添加或删除的动态字段。

我需要知道这些字段的 jquery 选择器,以便我可以在字段之间进行一些 javascript 计算,目前动态字段将当前日期时间设置为字段,人们通常如何处理动态字段验证?

<%= nested_form_for [@project, @quote], :html => {:class =>'form-inline'} do |f| %>
<div>
  <%= f.hidden_field :project_id %>
  <%= f.label :name %>
  <%= f.text_field :name %>
  <%= f.label :status %>
  <%= f.text_field :status %>
</div>
  <p>
    <h4> Labour Items </h4>
  </p>
  <p>
    <%= f.fields_for :labouritems, :html => { :class => 'form-vertical' } do |builder| %>

    <%= builder.label :description %>
    <%= builder.text_field :description, :class => "input-medium" %>
    <%= builder.label :hours %>
    <%= builder.text_field :hours, :class => "input-mini" %>
    <%= builder.label :total %>
    <%= builder.text_field :total, :class => "input-mini" %>
    <%= builder.label "Mup/Hr" %>
    <%= builder.text_field :muphourrate, :class => "input-mini" %>
      <%= builder.label "Mup/Total" %>
    <%= builder.text_field :muptotal, :class => "input-mini" %>
    <%= builder.label "Mup/%" %>
    <%= builder.text_field :muppercent, :class => "input-mini" %>
    <%= builder.label "Contractor" %>

    <%= builder.autocomplete_field :contractor_name, autocomplete_contractor_firstname_contractors_path, :update_elements => {:id => "##{form_tag_id(builder.object_name, :contractor_id)}" , :hourly_rate => "##{form_tag_id(builder.object_name, :contractor_rate)}"   },:class => "input-small",:placeholder => "Seach" %>
    <%= builder.hidden_field :contractor_id  %>
    <%= builder.label "Rate/hr"  %>
    <%= builder.text_field :contractor_rate,:class => "input-mini" %>
    <%= builder.label "Work %" %>
    <%= builder.text_field :progress_percent, :class => "input-mini" %>
    <%= builder.label "Work $" %>
    <%= builder.text_field :progress_payment, :class => "input-mini" %>

    <%= builder.link_to_remove "Delete",:class => 'btn btn-small btn-danger'   %>

  <% end %>

  <%= f.link_to_add "Add Item", :labouritems, :class => 'btn btn-small btn-primary' %>
</p>
4

1 回答 1

0

我不知道您需要验证什么类型的信息,但这样的事情应该会有所帮助。您要验证的所有字段都需要一个类或 ID。如果您需要以某种方式验证某个问题,请给它一个 ID 并使用$("#elements_id")jQuery 进行选择。

$(".form-inline").submit(function(){

  $.each($(".input-mini"), function(){
    if($(this).val() == ""){
      $("#warnings").html("<p> Ooops, you forgot something </p>"); // Warn
      return false;  // Don't submit
    }
  }

});
于 2012-10-14T14:38:00.663 回答