Ruby on Rails: Getting the value from a select form field before it's submitted

CottonEyeJoe

I'm trying to get the value of a select form field, before it's submitted and save the value into a variable. I know that you can't do it with Rails only. So JavaScript/Ajax will do the trick. I'm pretty new to Rails, so I hope you can help.

Richard Peck

before it's submitted and save the value into a variable

This is the realm of jquery/javascript -- anything on the client side is JS, not Rails.

Rails/Ruby is basically like PHP -- it runs on the server.

Although @Sudipta Mondal's answer will help, you have to remember to bind your code to an event (typically change):

#app/assets/javascripts/application.js
$(document).on("change", "select#form_item", function(e){
   $(this).attr("data-option-value", $(this).val()); // this sets the "data-option-value" to the value
});

Perhaps this isn't the context in which you intended; it should give you the ability to dynamically assign the relative value to the field.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

select values in a form not getting submitted

From Dev

getting value from user in form's text field html

From Dev

How to modify value from a form request before it is submitted in the DB?

From Dev

getting form value into controller in ruby on rails

From Dev

Having trouble getting <select> value to form field

From Dev

Ruby on Rails, simple_form - I want to set a hidden field of the simple_form from a html select_tag field

From Dev

Having trouble getting form field to have to database with Ruby on Rails

From Dev

Getting submitted form value for confirmation page

From Dev

Getting submitted form value for confirmation page

From Dev

Ruby on Rails form isn't capturing values in select multiple field

From Dev

Get a submitted value with AJAX (Ruby on Rails)

From Dev

Get a submitted value with AJAX (Ruby on Rails)

From Dev

Rails: In a controller, add a value to the params submitted by a form

From Dev

Preventing an HTML form field from being submitted

From Dev

How to make a stripe charge in rails form BEFORE form is submitted

From Dev

getting the index of the select_tag selected value in ruby on rails

From Dev

Before IFRAME form is submitted, save the parent url to hidden field

From Dev

Rails 4 form: display database value in select field

From Dev

Rails 4 form: display database value in select field

From Dev

Rails: Display Selected Value in Multi Select Form Field

From Dev

ruby form_for password field not submitted to params hash

From Dev

Get value from dynamic form field before postback with jQuery

From Dev

Ruby on Rails - Not Select a Given Field

From Dev

form is not getting submitted

From Dev

Form not getting submitted

From Dev

Can TestFlight's "What to test" field be edited before the build is submitted?

From Dev

getting all users from particular columns value in ruby on rails

From Dev

getting all users from particular columns value in ruby on rails

From Dev

Field value of integer doesn't get submitted (Simple form)

Related Related

  1. 1

    select values in a form not getting submitted

  2. 2

    getting value from user in form's text field html

  3. 3

    How to modify value from a form request before it is submitted in the DB?

  4. 4

    getting form value into controller in ruby on rails

  5. 5

    Having trouble getting <select> value to form field

  6. 6

    Ruby on Rails, simple_form - I want to set a hidden field of the simple_form from a html select_tag field

  7. 7

    Having trouble getting form field to have to database with Ruby on Rails

  8. 8

    Getting submitted form value for confirmation page

  9. 9

    Getting submitted form value for confirmation page

  10. 10

    Ruby on Rails form isn't capturing values in select multiple field

  11. 11

    Get a submitted value with AJAX (Ruby on Rails)

  12. 12

    Get a submitted value with AJAX (Ruby on Rails)

  13. 13

    Rails: In a controller, add a value to the params submitted by a form

  14. 14

    Preventing an HTML form field from being submitted

  15. 15

    How to make a stripe charge in rails form BEFORE form is submitted

  16. 16

    getting the index of the select_tag selected value in ruby on rails

  17. 17

    Before IFRAME form is submitted, save the parent url to hidden field

  18. 18

    Rails 4 form: display database value in select field

  19. 19

    Rails 4 form: display database value in select field

  20. 20

    Rails: Display Selected Value in Multi Select Form Field

  21. 21

    ruby form_for password field not submitted to params hash

  22. 22

    Get value from dynamic form field before postback with jQuery

  23. 23

    Ruby on Rails - Not Select a Given Field

  24. 24

    form is not getting submitted

  25. 25

    Form not getting submitted

  26. 26

    Can TestFlight's "What to test" field be edited before the build is submitted?

  27. 27

    getting all users from particular columns value in ruby on rails

  28. 28

    getting all users from particular columns value in ruby on rails

  29. 29

    Field value of integer doesn't get submitted (Simple form)

HotTag

Archive