问题
I'm having some trouble with a select
tag.
I want to assign a value to a variable depending on which name is shown on the select tag:
<%= select('product', 'name' , @products.map { |s| [s.name, s.id] }, {}, {:class => "form-control"}) %>
The values display correctly, but I want to assign the currently selected value to a variable so that it can be used later on the same page to display content with the relevant details of the selected product.
For example:
@product = currently_displayed_name
Is this possible with Rails and how to do it? Or do I might need to use another resource?
回答1:
Props to the comments, the answer is that Rails does not give you a way to do that. Depending on what you need to manipulate when user changes select value, you might be able to find a JavaScript library for it.
Otherwise, you can load the default variable on the page, and then handle the rest with an onchange
handler:
$('#product_name').change(function() {
// perform actions on the page with javascript
});
来源:https://stackoverflow.com/questions/36781540/how-to-assign-a-variable-the-current-output-of-a-select-tag