views:

429

answers:

3

I have a form with two text_fields:

<input type="text" id="post_name" name="post[name]" />
<input type="text" id="post_email" name="post[email]" />

And I have an observe_field on the name input box but I also want to pass the email value with it. In prototype this is done via:

<%= observe_field :post_name, 
:url => { :controller => :live_validations, :action => :validate_name },
:frequency => 0.5,
:update => :post_name_message,
:with => "Form.Element.serialize('post_name','post_email')" %>

But I'm using JRails. Is there a JQuery equivalent to Form.Element.serialize? Basically something to grab the values of both input boxes and assign it to a variable.

More info on this helper is here: http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html#M001939

A: 
$("form").serialize();
algiecas
assuming you want to serialize everything in the form, of course.
JacobM
A: 

JQuery has a serialize() command; it works on a set of fields that have been defined using a jQuery selector (or on a form, as in algiecas' example).

So in your case you would need to have some selector that defines all the fields, and only those fields, that you want to serialize. This might be as simple as "serialize all the input fields" ( $("input").serialize(); ) or could require adding markup such as placing a div with an id around the fields you care about.

JacobM
A: 

Here's a simple way to do it -- substitute $('post_name') with whatever JQuery uses to call getElementById() (I think it uses the same syntax as prototype?)

observe_field( :post_email, ..., 
:with => "'post_email='+encodeURIComponent(value)+'&post_name='+encodeURIComponent($('post_name').value)" %>
Julian