【问题标题】:Select Box Change Dependent Options dynamically动态选择框更改相关选项
【发布时间】:2018-01-29 00:47:45
【问题描述】:

我有一个问题,如果有人能帮我找到答案,我会很高兴的。

我有一个对象:

myObj = {
"red":{
    "10729,10730,10732":{
                "-2.75":["+1.75","+1.50","+1.25","+1.00","+0.75","+0.50","+0.25","-0.25"],
                "-2.50":["+2.00","0.00","-0.50","-0.75","-1.00","-1.25"],
                "-2.25":["+2.00","0.00","-1.50","-1.75","-2.00"],
                "-2.00":["+2.00","0.00","-2.00","-2.25"],
                "-1.75":["+2.00","0.00","-2.25"],
                "-1.50":["+2.00","0.00","-2.25"],
                "-1.25":["-2.25"],
                "-1.00":["+2.00","-2.25"],
                "-0.75":["+2.00","-2.25"],
                "-0.50":["+2.00","-2.25"],
                "-0.25":["-2.25"],
                "0.00":["+1.75","0.00","-2.25"],
                "+0.25":["-2.00","-2.25"],
                "+0.50":["+1.75","-1.75"],
                "+0.75":["+1.75","0.00","-1.50","-1.75"],
                "+1.00":["0.00","-1.50"],
                "+1.25":["+1.75","0.00","-1.25"],
                "+1.50":["+1.50","0.00","-1.00"],
                "+1.75":["0.00","-0.75","-1.00"],
                "+2.00":["+1.50","-0.50","-0.75"],
                "+2.25":["+1.50","-0.25","-0.50"],
                "+2.50":["+1.50","+0.25","0.00","-0.25"],
                "+2.75":["+1.50","+0.75","+0.50","+0.25"],
                "+3.00":["+1.75","+1.50","+1.25","+1.00","+0.75"],
                "+3.25":["+1.75","+1.50"]
    }
},

"gray":{
    "10730,10731":{
        "-1.00":["+0.25","0.00","-0.25","-0.50"],
        "-0.75":["+0.75","+0.50","+0.25","-0.50","-0.75"],
        "-0.50":["+0.75","-0.75"],
        "-0.25":["+0.75","-0.75"],
        "0.00":["+3.25","+3.00","+2.75","+2.50","+2.25","+1.00","+0.75","-0.50","-0.75","-1.50","-1.75"],
        "+0.25":["+1.00","-0.50"],"+0.50":["+1.00","-0.50"],"+0.75":["+1.00","-0.50"]
    }
}
};//end of myObj

我正在遍历它并将值作为选项附加到选择标记中 这是我的代码:

 for(h in myObj){ //h is color
    for (x in myObj[h]) {   //x is diameter
      for(y in myObj[h][x]){ //y is cylinder
        $(".cyl").append('<option value="">' + y + '</option>');               
         for(z in myObj[h][x][y]){  //// z is array of sph,and myObj[h][x][y][z] is the array elements
            $(".sph").append('<option value="">' + myObj[h][x][y][z] + '</option>');                
           }  ////sph
       }////cyl
    }
}

循环完美运行。我不想推送所有元素,而是推送数组值。或类似依赖选项列表的东西。 例如,用户选择第一个圆柱体"-2.75",第二个选择标签将仅显示其数组中的值

["+1.75","+1.50","+1.25","+1.00","+0.75","+0.50","+0.25","-0.25"]

有什么想法吗? 提前致谢。

【问题讨论】:

    标签: javascript jquery select dynamic


    【解决方案1】:

    var myObj = {
      "red": {
        "10729,10730,10732": {
          "-2.75": ["+1.75", "+1.50", "+1.25", "+1.00", "+0.75", "+0.50", "+0.25", "-0.25"],
          "-2.50": ["+2.00", "0.00", "-0.50", "-0.75", "-1.00", "-1.25"],
          "-2.25": ["+2.00", "0.00", "-1.50", "-1.75", "-2.00"],
          "-2.00": ["+2.00", "0.00", "-2.00", "-2.25"],
          "-1.75": ["+2.00", "0.00", "-2.25"],
          "-1.50": ["+2.00", "0.00", "-2.25"],
          "-1.25": ["-2.25"],
          "-1.00": ["+2.00", "-2.25"],
          "-0.75": ["+2.00", "-2.25"],
          "-0.50": ["+2.00", "-2.25"],
          "-0.25": ["-2.25"],
          "0.00": ["+1.75", "0.00", "-2.25"],
          "+0.25": ["-2.00", "-2.25"],
          "+0.50": ["+1.75", "-1.75"],
          "+0.75": ["+1.75", "0.00", "-1.50", "-1.75"],
          "+1.00": ["0.00", "-1.50"],
          "+1.25": ["+1.75", "0.00", "-1.25"],
          "+1.50": ["+1.50", "0.00", "-1.00"],
          "+1.75": ["0.00", "-0.75", "-1.00"],
          "+2.00": ["+1.50", "-0.50", "-0.75"],
          "+2.25": ["+1.50", "-0.25", "-0.50"],
          "+2.50": ["+1.50", "+0.25", "0.00", "-0.25"],
          "+2.75": ["+1.50", "+0.75", "+0.50", "+0.25"],
          "+3.00": ["+1.75", "+1.50", "+1.25", "+1.00", "+0.75"],
          "+3.25": ["+1.75", "+1.50"]
        }
      },
    
      "gray": {
        "10730,10731": {
          "-1.00": ["+0.25", "0.00", "-0.25", "-0.50"],
          "-0.75": ["+0.75", "+0.50", "+0.25", "-0.50", "-0.75"],
          "-0.50": ["+0.75", "-0.75"],
          "-0.25": ["+0.75", "-0.75"],
          "0.00": ["+3.25", "+3.00", "+2.75", "+2.50", "+2.25", "+1.00", "+0.75", "-0.50", "-0.75", "-1.50", "-1.75"],
          "+0.25": ["+1.00", "-0.50"],
          "+0.50": ["+1.00", "-0.50"],
          "+0.75": ["+1.00", "-0.50"]
        }
      }
    }; //end of myObj
    
    //Doc ready
    $(function() {
      fillColors();
    });
    
    // Change event of colors
    $(document).on('change', 'select[name=color]', function() {
      fillDiameters($(this).val());
    });
    // Change event of diameter
    $(document).on('change', 'select[name=diameter]', function() {
      fillCylinderrs($('select[name=color] option:selected').val(), $(this).val());
    });
    
    //function to fill all colors
    function fillColors() {
      $('select[name=color]').append($('<option>select</option>'));
      for (var property in myObj) {
        if (myObj.hasOwnProperty(property)) {
          $('select[name=color]').append($('<option value="' + property + '">' + property + '</option>'));
        }
      }
    }
    
    //function to fill diameter
    function fillDiameters(color) {
      $('select[name=diameter]').html('');
      $('select[name=cylinder]').html('');
      $('select[name=diameter]').append($('<option>select</option>'));
      for (var property in myObj[color]) {
        if (myObj[color].hasOwnProperty(property)) {
          for (var diameter in myObj[color][property]) {
            if (myObj[color][property].hasOwnProperty(diameter)) {
              $('select[name=diameter]').append($('<option value="' + diameter + '">' + diameter + '</option>'));
            }
          }
        }
      }
    }
    
    //function to fill cylinder
    function fillCylinderrs(color, paramDiameter) {
      $('select[name=cylinder]').html('');
      $('select[name=cylinder]').append($('<option>select</option>'));
    
      for (var property in myObj[color]) {
        if (myObj[color].hasOwnProperty(property)) {
          for (var diameter in myObj[color][property]) {
            if (myObj[color][property].hasOwnProperty(diameter) && diameter == paramDiameter) {
              var cylinders = myObj[color][property][diameter];
              for (var cyl in cylinders) {
                $('select[name=cylinder]').append($('<option value="' + cylinders[cyl] + '">' + cylinders[cyl] + '</option>'));
              }
            }
    
          }
        }
      }
    }
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <select name="color">
    </select>
    <select name="diameter">
    </select>
    <select name="cylinder">
    </select>

    你在寻找这样的东西吗?

    【讨论】:

    • 是的,非常感谢。但我不希望直径和圆柱体的选择标签依赖于颜色。我会尝试自己做。但如果我有问题,我可以将其留在 cmets 中吗?
    • 当然。很乐意做任何帮助,我可以。
    • 我有点怀疑。如果没有颜色选择,如果用户从直径选择框中选择“-1.00”,那么圆柱选择框中需要显示哪个值? ["+2.00", "-2.25"] 从红色或 ["+0.25", "0.00", "-0.25", "-0.50"] 从灰色?
    • 你是对的,但在我的情况下,只要该值在与该值对应的数组中,就会出现哪个值并不重要。
    • 我重写了你的代码,一切都很好。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2012-09-18
    • 2015-11-20
    • 2021-12-24
    • 1970-01-01
    • 1970-01-01
    • 2015-06-12
    • 2018-04-01
    • 1970-01-01
    相关资源
    最近更新 更多