Jeisson Posted February 16, 2016 Share Posted February 16, 2016 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 Quote Link to comment Share on other sites More sharing options...
requinix Posted February 16, 2016 Share Posted February 16, 2016 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> Quote Link to comment Share on other sites More sharing options...
Jeisson Posted February 16, 2016 Author Share Posted February 16, 2016 (edited) 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 February 16, 2016 by Jeisson Quote Link to comment Share on other sites More sharing options...
Psycho Posted February 16, 2016 Share Posted February 16, 2016 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. Quote Link to comment Share on other sites More sharing options...
Jeisson Posted February 16, 2016 Author Share Posted February 16, 2016 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.. Quote Link to comment Share on other sites More sharing options...
requinix Posted February 16, 2016 Share Posted February 16, 2016 You get the values from AJAX and then put them into the , right? Or something like that? Somehow you start with the array like you first posted and then turn it into the list. Whatever the method is, you do have both the label and the value so you should be producing list items like label Quote Link to comment Share on other sites More sharing options...
Jeisson Posted February 18, 2016 Author Share Posted February 18, 2016 started a new topic on xml -> SELECT (option) with jquery n example Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.