I'm using a fields_for
helper in rails wi开发者_如何学JAVAth some text boxes. How can I get the ID of these text boxes to use in some Javascript? I would like to be able to manipulate the status of the other checked boxes by clicking on certain boxes.
e.g.
<% fields_for "[id][]", app do |fields| %>
<%= fields.check_box :featured %>
...
Then do something with scriptaculous or an onclick
to deal with what happens to other generated checkboxes.
Predicting ids gets too messy on dynamic forms for reliable javascript.
Instead you can use the prototype helpers and class names to achieve similar functionality.
Make this javascript available to your page:
var myrules = {
'.highlightFeatured': function (e){
clicked_on_box = Event.element(e)
container = clicked_on_box.up('form')
box_to_modify = container.down('.featured')
new Effect.Highlight(box_to_modify)
};
};
Event.observe(window, 'load', function(){
$('fields').delegate('click', myrules);
});
And define your view along these lines. Now when the bogus check box is clicked featured will be highlighted.
<% fields_for "[id][]", app do |fields| %>
<div id="fields">
<%= fields.check_box :featured, :class => "featured" %>
<%= fields.check_box :bogus, :class => "highlightFeatured" %>
...
</div>
<% end %>
精彩评论