Pogopuschel
Pogopuschel

Reputation: 133

input[type=text] submit different value than displayed

I am developing an app using Rails and jQuery. I would like to submit an id integer value via an input form but still display a friendly text value to the user while he is filling out the form. The text input field is using jQuery autocomplete so once the user chooses an entry, an integer value will be put into the input field. Is there any way to display a different value from what is actually submitted?

Upvotes: 5

Views: 8736

Answers (4)

rags
rags

Reputation: 534

Alternatively, you can also keep a hidden input field, and use a "change" event to keep the hidden input field updated every time the user changes the original field.

Upvotes: 2

IOrlandoni
IOrlandoni

Reputation: 1828

Try using the combobox functionality for the jQuery UI Autocomplete:

http://jqueryui.com/demos/autocomplete/#combobox

The value attribute of the options are what get sent along with the form, not the display text.

Upvotes: 1

Rushabh Ajay Hathi
Rushabh Ajay Hathi

Reputation: 107

I understand what you want to do. I have done the same thing for a drop down. Never tried for a text box though!:( What I did was that the dropdown displayed name of entries in database and when user submits the form,the ID in the database is passed. I hope thats precisely what you want. I am putting the code for the same below. I know this is not what you are looking for but it may give you a hint !

In the view:

<%= f.select :facilitator_group_id, FacilitatorGroup.all.map { |fg| [fg.name, fg.id] } %>

FacilitatotGroup is my model name.

Cheers !

Hi.I also feel that the reason we use textfield is for user to enter data which they wanna store in DB. For your reason, why cant you use a dropdown ?

Upvotes: 1

Daniel
Daniel

Reputation: 31579

You can use

$('myform').submit(function() { /* your thing here */ });

And your thing will run when the user submits.

Upvotes: 0

Related Questions