How to assign a variable the current output of a select tag

时间秒杀一切 提交于 2019-12-11 10:47:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!