I'm creating a form in a Rails 3 app that has a dropdown field. In the dropdown there is an option for "Other", and when you select "Other" a text box appears, and when saved, the text box value is saved instead of the dropdown menu value. I wrote some javascript that accomplishes the ab开发者_如何学运维ove for any select dropdown with the class "has_other".
I'm having trouble figuring out how to reverse this, i.e. when a user is editing the form with existing contents in it, if the field in the database is something other than what is in the select menu, then to display "Other" and the textbox with the custom value in it. I would like to do this via javascript if possible, so I can still have one constant function that works on all select boxes with class = "has_other". Right now when I view the edit form with data in it, if the value stored in the database is not in the dropdown menu, the first item is selected (as it would be if nothing was selected yet).
My question is, is there some way via javascript/jQuery that I could check the "attempted" value of the dropdown so I could use that to manipulate the actual value? Such as, on page load, get the data that the rails object is putting into the form? Sorry if this is unclear, I'll edit if more info would be useful.
Thanks in advance.
The dropdown is a list of strings that operate as a suggestion? If it was a lookup-field i would assume you would need two fields.
So if that is the case, i would suggest to temporarily add the manually entered element to the dropdown values. That way:
- the value is visible in the correct place
- the rest of the interface keeps on working as expected (if they want to change/edit it, they still need to select 'other' again).
Hope this helps.
精彩评论