Getting value of a hidden field in Rails with JQuery
I have a problem in my Rails Project. It runs on Rails 2 by the way.
<%= form.hidden_field :foo %>
Is it possible to get the value of this hidden field with jQuery? Maybe someth开发者_如何学JAVAing like this:
var foo = jQuery('hidden_field').val();
Any Ideas?
I would use the ':hidden' selector in jQuery ( http://api.jquery.com/hidden-selector/ ). To expand on @Koraktor's examples:
var foo = jQuery('#foo:hidden').val();
or
var foo = jQuery('form#some_form input[name="foo"]:hidden').val();
You will have to use the ID of the field (or some other unique selectors):
var foo = jQuery('#foo').val();
or
var foo = jQuery('form#some_form input[name="foo"]').val();
PS: Getting the value of a hidden is nothing different from a normal field. Hiding a field is a pure interface decision.
A rails hidden field is identical to a rails non-hidden field.
jQuery("[name=foo]")
will get that field.
For nested forms you can get all inputs of those sorts by jQuery("[name $= '[foo]'")
.
There are also queries for hidden fields like jQuery(":input:hidden")
:input selects input, select, textarea, button vs just input elements..
In the end <input type="hidden"/>
is identical to <input type="text"/>
when it comes to name selectors or id selectors or anything.
精彩评论