简体   繁体   English

JavaScript数组是否基于下拉选择自动填充?

[英]JavaScript Array Auto Populates Based On Dropdown Selection?

So I have a question about a web app I'm considering making. 所以我对正在考虑制作的Web应用程序有疑问。 I want to allow users to select an item from a dropdown list. 我想允许用户从下拉列表中选择一个项目。 For instance we'll say clothes. 例如,我们说衣服。 So they select an option from the dropdown list like: Shirt, Pants, Shoes. 因此,他们从下拉列表中选择一个选项,例如:衬衫,裤子,鞋子。

From there, it will then auto-populate various input/text fields below such as prices. 然后,它将从那里自动填充下面的各种输入/文本字段,例如价格。

Do I make one array with all of the options? 我是否可以使用所有选项制作一个阵列? Such as {'shirt', 'pants', 'shoes'}, or do I do: {'shirt', '14.99', 'red', 'tuesday'} 例如{'shirt','pants','shoes'}或我该怎么做:{'shirt','14.99','red','tuesday'}

For instance if someone selected shirt, it would then populate the fields based on $14.99 for price, red for color, and Tuesday for date to wear. 例如,如果有人选择了衬衫,它将根据价格(14.99美元),红色(颜色)和星期二(穿着日期)填充字段。

OR do I do a different array for each individual field and just do if then statements like, "if 'shirt' === 'red', then [0]" 或我为每个单独的字段做一个不同的数组,然后执行if语句,例如“ if'shirt'==='red',然后[0]”

I'm lost on how to get started ( extremely inexperienced in JS, but figured I'd ask.) 我迷上了入门方法(JS经验不足,但是我想问一下。)

Thanks in advance. 提前致谢。

I'm writing a small piece of code to give you an idea. 我正在写一小段代码给您一个想法。 I found time and I also write select boxes. 我找到了时间,还写了选择框。

 var dress, i, j, x = ""; dress = { "shirt": [ { "product_name":"women shirt", "price": 25, "colors":[ "red", "green", "yellow" ] }, { "product_name":"baby shirt", "price": 35, "colors":[ "red", "pink" ] }, { "product_name":"man shirt", "price": 15, "colors":[ "pink", "blue" ] } ] } for (i in dress.shirt) { x += "<h2>Product Name: " + dress.shirt[i].product_name +" > Price: "+ dress.shirt[i].price+"</h2>"; for (j in dress.shirt[i].colors) { x += dress.shirt[i].colors[j] + "<br>"; } } document.getElementById("demo").innerHTML = x; 
 <p id="demo"></p> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM