javascript - Setting a value of a text field from a 2D array depending on the option of a dropdown menu -
I have a dropdown menu that provides options for the United States.
The array of states is declared the controller in this form: with the Now I want to set text_field with id = code> electr based on selected state. That is, if 'AL' is chosen, then as a jQuery: How can I get this? I am new to Ruby and Ruby on MVC, so please support me < / P> Edit: This is a state dropdown code: And this is text_field that should be set: And this is the jQuery code segment, which was proposed by @edney, added with a warning: I do not even see the warning. Take a look at this jsFiddle for a solution: You have to populate the javascript variable @us_states = [['Alabama ',' AL '], [' Alaska '] now , I have another array (also as the controller) in which there is a number for each state, in this form:
@ Electricity_table = [['Alabama', 0.078345], [ 'Alaska', 0.1211736], ['Arizona', 0.0773004]]
change event, via jQuery, I
warnings to see for states Value Rropdaun menu is the second column (ie
AL ,
AK ).
$ ('# electr'). Val (0.078345);
<%> f.input : Var_state_oth, label: false, wrapper :: andend%> & Lt;% = f.input_field: var_state_oth, archive: @states, category: 'answer-singles', id:' ap_ar_state_th '%' gt; & Lt;% end% & gt;
<% = f.input: electr, label: wrong, wrap :: attach Do% 1 & Gt; & Lt; Input class = "answer-single" data -autonumeric = "true" data-M-DC = "0" id = "electric" name = "app [electric]" type = "text" value = "& lt;% = @App.var_energy_cons_reduction% & gt; "& gt;
$ ('# var_state_oth' ). ('Change', function () {warning ("detected change"); var val = $ ('option: selected', this) .text (); $ ('# electr option') .prop ('selected' , False) .filter (function () {refund $ (this) .text (). IndexOf (= val)! = -1}) .prop ('selected', true);});
$ ('# Var_state_oth'). ('Change', function () {var sel = $ ('option: selected', $ (this)). Text (); $ ('# electrator'). Val (elec_rates [sel]);});
elec_rates with the data from
@ electricity_table . :
& lt; Script type = "text / javascript" & gt; & Lt;% = var elec_rates = # {@ electricity_table.to_json}; & Gt%; & Lt; / Script & gt;
Comments
Post a Comment