使用 JQuery 从数组填充下拉列表
假设我有这个数组:
Array(
[0] => Array(
[Brand] => 'Toyota',
[Model] => 'Camry',
[Color] => 'Red',
[Year] => 2002
)
[1] => Array(
[brand] => 'Nissan',
[Model] => 'Skyline',
[Color] => 'White',
[Year] => 2005
)
[2] => Array(
[Brand] => 'Honda',
[Model] => 'Civic',
[Color] => 'Green',
[Year] => 2000
) )
然后在下拉列表中我有“键”品牌+型号+颜色+年份
问题:如何使用JQuery(AJAX)填充另一个下拉列表,当我单击时使用“值”一键?
示例:当我单击 Model 时,我想用值 Camry + Skyline + Civic,如果我点击颜色,则会填充红色 + 白色 + 绿色等等
干杯!
Assuming I have this array:
Array(
[0] => Array(
[Brand] => 'Toyota',
[Model] => 'Camry',
[Color] => 'Red',
[Year] => 2002
)
[1] => Array(
[brand] => 'Nissan',
[Model] => 'Skyline',
[Color] => 'White',
[Year] => 2005
)
[2] => Array(
[Brand] => 'Honda',
[Model] => 'Civic',
[Color] => 'Green',
[Year] => 2000
) )
and then inside a drop down list I have the "keys" Brand + Model + Color + Year
Question: how can I populate another drop down list using JQuery(AJAX), with the "values" when I click on one key?
Example: When I click on Model, I want to populate another drop down list with the values Camry + Skyline + Civic, and if I click Color, pupulates with Red + White + Green and so on
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您的数组是
cars[]
并且您的选择框的 ID 分别为“sourceBox”和“targetBox”,则以下代码将获取 #sourceBox 中所选项目的文本,并使用所有内容填充 #targetBox数组中的等效值。因此,在#sourceBox 中选择“颜色”将会用“红色”、“白色”和“绿色”填充#targetBox。If your array is
cars[]
and your select boxes have IDs of "sourceBox" and "targetBox" respectively, the following code will get the text of the selected item in #sourceBox and populate #targetBox with all the equivalent values from your array. So selecting "color" in #sourceBox will populate #targetBox with "red", "white", and "green."只是一段非常通用的代码。您应该更具体(显示您想要从该 PHP 数组创建的 HTML)。不管怎样,就这样吧。这只会使用 PHP 数组中的值创建 JS 数组。那么与这些人一起工作应该很容易:
Just a very general piece of code. You should be more specific (show HTML you want created from that PHP array). Anyways here it goes. This will just create JS arrays with values from your PHP array. It should be easy to work with those then: