ruby on rails - Selecting a value from the select box when options added by an ajax call -
I have a form of adding recipes, in the same way the user can add multiple recipe content; Every Agentis user will call AJAX This way, I do not need to display the content selection box. When the user selects a component, the ID of the selected content is stored in the hidden input
<% = builder.hidden_field: ingredient_id%> & Lt; / Div & gt;
When the user selects a component from the list, then Ajax triggers the call to populate another selection box which displays all the sizes of the content. This set up works perfectly when I'm making a new dish.
I have a problem when I open the existing recipe in editing mode ...
I have the following HTML
jQuery code
jQuery.fn.bindServingSizes = function (element) {var $ this = $ (this); $ .getJSON ("/ serving_sizes? Ingredientid =" + ingredientid, function (j) {var option = "& lt; option value = '> & gt; please select a serving size & lt; / option & gt;"; ( Var i = 0; I & lt; j.length; i ++) option + = '& lt; option value =' '+ [j] [i] .serving_size.id +' "'gt;' + [ I] Protection_Size Name + '& lt; / option & gt;' $ $ (optional);}); };
I have an AJAX call that runs when the page is loaded, reads the value in the hidden input and receives the servicing size and adds it as an option in the selection box < Strong> My question : How do I choose the right option in the selection box? I have the data in column "serving_size_id", , but I do not know how to get the value of it, so I can set the right selected item with value.
I hope that I am clear enough in my interpretation.
I'm not a jQuery expert at all, but for your example, I see that you have available options Make HTML for the supply.
So you can add that option that matches the value of serving_size_id
. Using jQuery, a value can be obtained from serving_size_id
:
var serving_size = $ ("# serving_size_id"). Val ();
This value van is used in the loop for 'Find' in your options.
Comments
Post a Comment