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: @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]]   

with the change event, via jQuery, I warnings to see for states Value Rropdaun menu is the second column (ie AL , AK ).

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:
$ ('# electr'). Val (0.078345);

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:

  <%> 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;   

And this is text_field that should be set:

  <% = 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;   

And this is the jQuery code segment, which was proposed by @edney, added with a warning:

  $ ('# 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);});   

I do not even see the warning.

Take a look at this jsFiddle for a solution:

  $ ('# Var_state_oth'). ('Change', function () {var sel = $ ('option: selected', $ (this)). Text (); $ ('# electrator'). Val (elec_rates [sel]);});   

You have to populate the javascript variable 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

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -