Ruby on Rails Send form information to Javascript script
This shouldn't be a difficult problem to answer, but I can't find my answer from searching google.
I'm trying to have a form that tells me how many characters you have remaining. I got the code working in basic html, but I'm having difficulty converting it into rails.
In html, the relevent piece of code looks like:
<textarea name="content" onKeyUp="rem_char(this.form.content);"></textarea>
In Rails, the code looks like:
<%= f.text_area :content, :onkeyup => "rem_char(this.form.content);" %>开发者_运维技巧;
I can tell from viewing the source of the Rails document that the name of textarea is "micropost[content]", but changing "this.form.micropost[content]" does not work. In addition, adding :name => "content", causes the form input to not be read.
<%= f.text_area :content, :onkeyup => "rem_char(this);" %>
When that JS runs, this
is the textarea. You then ask for the textarea's form, then ask the form for the element named content
, which is the textarea. So save yourself the name dependent roundtrip there entirely, and just use this
.
Alternatively, use id
, not name
.
<%= f.text_area :content,
:onkeyup => "rem_char(document.getElementById('micropost_content'));" %>
精彩评论