阶段: 我使用的是嵌套模型,因此我们有两个模型:person
和car
。 在这里你有new.html.erb
页:如何知道Rails 3上html字段的id?
<%= form_for(@person do |f| %>
...
<%= f.fields_for :cars do |car| %>
<div class="field">
<%= car.label :color %><br />
<%= car.text_field :color, :maxlength => 7 %>
</div>
<% end %>
...
<% end %>
该代码会产生每节车厢:
<form ...>
...
<div class="field">
<label for="person_cars_attributes_0_color">Car color</label><br />
<input id="person_cars_attributes_0_color"
name="person[cars_attributes][0][color]"
size="7" type="text" />
</div>
...
</form>
现在假设我们需要把一些JavaScript代码,每个输入字段,产生像这样:
<script type="text/javascript">
$(document).ready(function() { $('#person_cars_attributes_0_color').mycolorpicker(); });
</script>
<input id="person_cars_attributes_0_color"
name="person[cars_attributes][0][color]"
size="7" type="text" />
注意,我们需要在JavaScript代码输入字段id
(在本例person_cars_attributes_0_color
)。
问题:我们如何获得每个html字段生成的id值?
非常感谢您的帮助。 如果您需要更多详细信息,请随时问我任何问题。
It太容易了,非常感谢,我在JavaScript和jQuery世界里也很喜欢这里。 – Israel