Jump to content
Jeisson

Select value from array

Recommended Posts

Hello there friends

 

I have an select box and I would like to define the alue of the selects with array. Like this:

 var price_list= new Array();
 price_list["-"]=0;
 price_list["bronze"]=5;
 price_list["silver"]=10;
 price_list["gold"]=15;

The I have a Selectbox where

<form action="" id="metal" onsubmit="return false;"
            <select id="a" name="metals" class="form-select">
                    <option value="-">-</option>
                    <option value="bronze">bronze</option>
                    <option value="silver">silver</option>
                    <option value="gold">gold</option>
            </select>

And performig calculations with following script (that works fine if the value is directly in select). And obviously there is another select (#b) but that is just numbers so I didn show it here.

  $(document).ready(function() {
           function compute() {
              var a = $('#metals').val();
              var b = $('#b').val();
              var total = a * b * 1.22;
              $('#result').text(total);
            }

            $('#metals, #b').change(compute);
            
         });

I just get returned NaN

If you have an answer then please hit

Share this post


Link to post
Share on other sites

The value of that metals thing is the value of the selected option. So "-" or "bronze" or "silver" or "gold".

 

If you want the value to be a number then make the value be a number.

<option value="0">-</option>
<option value="5">bronze</option>
<option value="10">silver</option>
<option value="15">gold</option>

Share this post


Link to post
Share on other sites

I get that. But wont work in my case. I need the value to be dynamical. I will get it from a json or similar. So Now I thought to get it from an Array. Afterwards I parse the json to that specific array.

Edited by Jeisson

Share this post


Link to post
Share on other sites

If you get the list via JSON (or some other method) then you could update the select list with the values and labels from that JSON list.

Share this post


Link to post
Share on other sites

ok. maybe I should directly do it that way then. i just thought i could "manually" do like I was asking first.

I know how to get json to array. havent done before select populsting or what should i call it..

Share this post


Link to post
Share on other sites

You get the values from AJAX and then put them into the

 

Whatever the method is, you do have both the label and the value so you should be producing list items like

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.