Reading rails text_field names with javascript
Here is what I'm trying to do
<% form_for @color, :html => {:multipart => true, :name => "color_form"} do |f| %>
<%= f.text_field :head_color %>
and I want to use this javascript
document.color_form.color[head_color].value;
javascript obviously doesn't like this and throws an error. The problem is 开发者_StackOverflow中文版that I can't use text_field_tag. How can I get javascript to read this?
Your current code acts as if the DOM treats array_field[names]
in any sort of special way, making document.color_form.color
an array. Nope, it's not quite that clever.
You can still access the field by name, though, using bracket notation.
document.color_form["color[head_color]"].value
used getElementById
, duh... much easier
精彩评论