MySQL表填充一个dropown。选择后,将填充第二个下拉列表 [英] MySQL table populating one dropsown. Upon selection, A second dropdown is populated

查看:134
本文介绍了MySQL表填充一个dropown。选择后,将填充第二个下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经在这里检查了网络和其他问题,我很难过。我试过了一个来自这里发布的问题的JavaScript解决方案,但我认为它不喜欢MySQL填充< option> s。我会复制所有我已经获得的JavaScript代码。



脚本:

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

PHP第一个下拉列表:

 < select class =form-controlid =groups> 
<?php
$ sql =SELECT BoilerBrand FROM boilerbrands;
$ result = mysql_query($ sql);
while($ row = mysql_fetch_array($ result)){
echo< option value ='。$ row ['ID']。'>。$ row ['BoilerBrand' ]。 < /选项> 中;
}
?>
< / select>

PHP第二个下拉列表

 < select class =form-controlid =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'] '> 中$行[ 'BoilerModel'] < /选项> 中。
}
?>
< / select>

任何帮助都将不胜感激!



感谢:)

解决方案

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

脚本:

 < script> 
$(function(){
<?php
$ sql =SELECT * FROM boilermodels;
$ result = mysql_query($ sql);
$ models = array();
while($ row = mysql_fetch_array($ result)){
$ models [$ row ['BoilerBrand']] [] = $ row;
}
/ *应该看起来像
$ models = [];
$ models [1] [] = ['ModelID'=>'1','BoilerBrand'=>'1', 'BoilerModel'=>'240E','BoilerGC'=>'47 -777-77','BoilerImage'=> '47 -777-77.jpg'];
$ models [1] BoilerModel ='290D','BoilerGC'=>'11 -111-11','BoilerImage'=''ModelID'=>'3','BoilerBrand'=>'1' '=> '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(value)['BoilerGC']''>' ,value ['BoilerModel'],'< / option>'));
});

sub.append(appendList);
});

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

第一个下拉列表:

 < select class =form-controlid =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' ]。 < /选项> 中;
}
?>
< / select>

第二个下拉列表:

 < select class =form-controlid =sub_groups> 
< option value =>选择模型< / option>
< / select>


Ok, so I've checked the net and the other questions on here and I'm stumped. I've tried a javascript solution from a question posted on here but I think it's not liking MySQL populating the <option>s. I'll copy all the code I've got including the javascript I have.

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 1st dropdown:

<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 2nd dropdown

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

Any help with this would be greatly appreciated!

Thanks :)

解决方案

The way I normally do this is instead of hiding/showing the options I remove/add them. I believe if you hide the options then the select input can still have that value.

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>

1st Dropdown:

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

2nd Dropdown:

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

这篇关于MySQL表填充一个dropown。选择后,将填充第二个下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆