使用 JQuery 从数组填充下拉列表

发布于 2024-09-14 15:46:08 字数 958 浏览 9 评论 0原文

假设我有这个数组:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哆兒滾 2024-09-21 15:46:08

如果您的数组是 cars[] 并且您的选择框的 ID 分别为“sourceBox”和“targetBox”,则以下代码将获取 #sourceBox 中所选项目的文本,并使用所有内容填充 #targetBox数组中的等效值。因此,在#sourceBox 中选择“颜色”将会用“红色”、“白色”和“绿色”填充#targetBox。

    $(document).ready(function(){
       $('#sourceBox').change(function(){
        var options = "";
        for (var i = 0; i < cars.length; i++){
          options += '<option>' 
              + cars[i][$('#sourceBox option:selected').text()] 
              + '</option>';
        }
        $('#targetBox').html(options);
        });
    });

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."

    $(document).ready(function(){
       $('#sourceBox').change(function(){
        var options = "";
        for (var i = 0; i < cars.length; i++){
          options += '<option>' 
              + cars[i][$('#sourceBox option:selected').text()] 
              + '</option>';
        }
        $('#targetBox').html(options);
        });
    });
不寐倦长更 2024-09-21 15:46:08

只是一段非常通用的代码。您应该更具体(显示您想要从该 PHP 数组创建的 HTML)。不管怎样,就这样吧。这只会使用 PHP 数组中的值创建 JS 数组。那么与这些人一起工作应该很容易:

<?php

$brands = array();
$models = array();
$colors = array();
$years = array();
foreach ($arr as $k=>$v) {
    switch ($k) {
        case 'Brand':
            $brands[] = $v;
            break;
        case 'Model':
            $models[] = $v;
            break;
        case 'Color':
            $colors[] = $v;
            break;
        case 'Years':
            $years[] = $v;
            break;
    }
}

?>

<script type="text/javascript">
    //<!--
$(document).ready(function() {

    var brands = new Array(<?php echo count($brands); ?>);
    var models = new Array(<?php echo count($models); ?>);
    var colors = new Array(<?php echo count($colors); ?>);
    var years = new Array(<?php echo count($years); ?>);

<?php

$i = 0;
foreach ($brands as $v) {
    echo 'brands[' . $i. '] = ' . $v . ';'
    ++$i;
}
$i = 0;
foreach ($models as $v) {
    echo 'models[' . $i. '] = ' . $v . ';'
    ++$i;
}
$i = 0;
foreach ($colors as $v) {
    echo 'colors[' . $i. '] = ' . $v . ';'
    ++$i;
}$i = 0;
foreach ($years as $v) {
    echo 'brands[' . $i. '] = ' . $v . ';'
    ++$i;
}

?>

    // now you should have items from your PHP array in JS arrays so you can do something with them

    $('a.populate-brands').click(function() {
        for (v in brands) {
            //
        } 
    })

});    //-->
</script>

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:

<?php

$brands = array();
$models = array();
$colors = array();
$years = array();
foreach ($arr as $k=>$v) {
    switch ($k) {
        case 'Brand':
            $brands[] = $v;
            break;
        case 'Model':
            $models[] = $v;
            break;
        case 'Color':
            $colors[] = $v;
            break;
        case 'Years':
            $years[] = $v;
            break;
    }
}

?>

<script type="text/javascript">
    //<!--
$(document).ready(function() {

    var brands = new Array(<?php echo count($brands); ?>);
    var models = new Array(<?php echo count($models); ?>);
    var colors = new Array(<?php echo count($colors); ?>);
    var years = new Array(<?php echo count($years); ?>);

<?php

$i = 0;
foreach ($brands as $v) {
    echo 'brands[' . $i. '] = ' . $v . ';'
    ++$i;
}
$i = 0;
foreach ($models as $v) {
    echo 'models[' . $i. '] = ' . $v . ';'
    ++$i;
}
$i = 0;
foreach ($colors as $v) {
    echo 'colors[' . $i. '] = ' . $v . ';'
    ++$i;
}$i = 0;
foreach ($years as $v) {
    echo 'brands[' . $i. '] = ' . $v . ';'
    ++$i;
}

?>

    // now you should have items from your PHP array in JS arrays so you can do something with them

    $('a.populate-brands').click(function() {
        for (v in brands) {
            //
        } 
    })

});    //-->
</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文