开发者

Reading rails text_field names with javascript

开发者 https://www.devze.com 2023-01-19 08:42 出处:网络
Here is what I\'m trying to do <% form_for @color, :html => {:multipart => true, :name => \"color_form\"} do |f| %>

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

0

精彩评论

暂无评论...
验证码 换一张
取 消