MySQL表填充一个dropown。选择后,将填充第二个下拉列表

时间:2016-11-22 13:00:12

标签: javascript php jquery mysql forms

好的,所以我在这里检查了网和其他问题,我很难过。我从这里发布的问题尝试了一个javascript解决方案,但我认为它不喜欢填充<option>的MySQL。我会复制我所拥有的所有代码,包括我的javascript。

SCRIPT:

<script>
  $(function() {
    $('#groups').on('change', function() {
      var val = $(this).val();
      var sub = $('#sub_groups');
      $('option', sub).filter(function() {
        if (
          $(this).attr('data-group') === val || $(this).attr('data-group') === 'SHOW'
        ) {
          $(this).show();
        } else {
          $(this).hide();
        }
      });
    });
    $('#groups').trigger('change');
  });
</script>

PHP第一次下拉:

<select class="form-control" id="groups">
  <?php
        $sql = "SELECT BoilerBrand FROM boilerbrands";
        $result = mysql_query($sql);
        while ($row = mysql_fetch_array($result)) {
          echo "<option value='".$row['ID']."'>".$row['BoilerBrand']."</option>";
        }
        ?>
</select>

PHP第二次下拉

<select class="form-control" id="sub_groups">
  <option data-group='SHOW' value="0">Model</option>
  <?php
     $sql = "SELECT * FROM boilermodels";
     $result = mysql_query($sql);
     while ($row = mysql_fetch_array($result)) {
      echo "<option data-group='".$row['BoilerBrand']."' value='".$row['BoilerGC']."'>".$row['BoilerModel']."</option>";
     }
     ?>
</select>

非常感谢任何帮助!

谢谢:)

1 个答案:

答案 0 :(得分:1)

我通常这样做的方式是隐藏/显示我删除/添加它们的选项。我相信如果你隐藏option s,那么select输入仍然可以具有该值。

SCRIPT:

<script>
    $(function(){
        <?php
        $sql = "SELECT * FROM boilermodels";
        $result = mysql_query($sql);
        $models = array();
        while ($row = mysql_fetch_array($result)) {
            $models[$row['BoilerBrand']][] = $row;
        }
        /* should look like
        $models = [];
        $models[1][] = ['ModelID'=>'1','BoilerBrand'=>'1','BoilerModel'=>'240E','BoilerGC'=>'47-777-77','BoilerImage'=>'47-777-77.jpg' ];
        $models[1][] = ['ModelID'=>'3','BoilerBrand'=>'1','BoilerModel'=>'290D','BoilerGC'=>'11-111-11','BoilerImage'=>'11-111-11.jpg' ];
        $models[2][]= ['ModelID'=>'2','BoilerBrand'=>'2','BoilerModel'=>'250E','BoilerGC'=>'47-777-77','BoilerImage'=>'47-777-77.jpg' ];
        */
        ?>
        var _boilermodels = '<?php echo json_encode($models); ?>';
        var jsonBoilerModels = JSON.parse(_boilermodels);
        console.log(jsonBoilerModels);

        $('#groups').on('change', function(){
            var $this = $(this);
            var val = $this.val();
            var sub = $('#sub_groups');
            sub.find('option').remove();
            var appendList = [];

            $.each(jsonBoilerModels[val],function(key,value){
                appendList.push('<option value="'.concat(value['BoilerGC'], '">', value['BoilerModel'], '</option>'));
            });

            sub.append(appendList);
        });

        $('#groups').trigger('change');
    });
</script>

第一次下拉:

<select class="form-control" id="groups">
    <?php
        $sql = "SELECT ID ,BoilerBrand FROM boilerbrands";
        $result = mysql_query($sql);
        while ($row = mysql_fetch_array($result)) {
            echo "<option value='".$row['ID']."'>".$row['BoilerBrand']."</option>";
        }
    ?>
</select>

第二次下拉:

<select class="form-control" id="sub_groups">
    <option value="">Select A Model</option>
</select>