James Ding James Ding - 2 months ago 16
Javascript Question

Ruby on rails: select_tag how to display based on selected value

Now my case might be a bit weird. I have a select_tag, user has to select one value from the list I gave. I know how to retrieve the value they selected once they click on the submit. But now I would also like to display some info for the selected value BEFORE they click on submit, just when they select any value in the drop down list.

So is there any way to get the value after user selects but before user submits ?

E.g:

select_tag("a", options_for_select(my_option_list))


the
param[:a]
will have the value just after user submits this. How can I get the value before they submit?

Answer

It depends what you want to do but you could use javascript.

Check out the fiddle

<select id="my_select">
    <option value="value 1">Option 1</option>
    <option value="value 2">Option 2</option>
    <option value="value 3">Option 3</option>
    <option value="value 4">Option 4</option>
</select>


$(function(){
    $('#my_select').on('change', function(){
        alert('do soemthing ' + $(this).val());
    });
});
Comments