简体   繁体   English

CLASS =“选择选择”的相关选择

[英]DEPENDENT SELECTS WITH CLASS = “chosen-select”

I'm trying to make a form with dependent dropdown, but the problem is when I go from select 1 to select 2 that has the "chosen-select" class, it does not show me the data in this dropdown, but if I change this class to this dropdown and I put "form-control", if you pass me the necessary data. 我正在尝试制作一个具有依赖下拉列表的表单,但是问题是当我从选择1转到具有“选择的选择”类的选择2时,它不会显示此下​​拉列表中的数据,但是如果我更改这个类到这个下拉菜单,如果您向我传递了必要的数据,我会放“ form-control”。

How can I use the "chosen-select" class in SELECT 2, so that I can read the data dynamically 如何在SELECT 2中使用“选择选择”类,以便可以动态读取数据

Select 1 选择1

<select id="jornada" class="form-control" name="jornada" required>
            <option value="">-- SELECCIONE --</option>
            <?php 
                    while ($row=mysqli_fetch_array($jornada)) {                     
                    echo '<option value='.$row["id"].'>'.$row["jornada"].'</option>';
                    }
                    mysqli_free_result($jornada)
             ?>            
</select>

Select2 - the ideal is that in this dropdown I can search, instead of selecting Select2-理想的情况是,我可以在此下拉列表中进行搜索,而不是选择

<select id="r_ruta" class="chosen-select" name="r_ruta" required>
    <option value="">-- SELECCIONE --</option>
</select>

Script 脚本

$(document).ready(function(){
        $("#jornada").change(function(){
            $.get("get_colegios.php","jornada_id="+$("#jornada").val(), function(data){
                $("#r_ruta").html(data);
                console.log(data);
            });
        });
});

I try whits this script and addClass() jquery, but not work 我尝试使用此脚本和addClass()jQuery,但无法正常工作

$(document).ready(function(){
    $("#jornada").change(function(){
        $.get("get_colegios.php","jornada_id="+$("#jornada").val(), function(data){         
            $("#r_ruta").html(data);            
            console.log(data);
            $("#r_ruta").addClass("chosen-select");
        });
    });
});

Im noob in this, very thnks for u help 我是菜鸟,非常感谢您的帮助

since you mentioned you would like to search as well as selecting you could use select2. 因为您提到了您想要搜索和选择,所以可以使用select2。 https://select2.org/ then you can change your dependent dropdown data source like this https://select2.org/,然后您可以像这样更改依赖的下拉数据源

$('#example').on('select2:select', function (e) {
    var data = e.params.data;
    if (data.text == 'Dogs'){
         $('#dependent').select2('destroy').empty().select2({data: dogs});
    }
    if (data.text == 'Birds'){
         $('#dependent').select2('destroy').empty().select2({data: birds});
    }
    if (data.text == 'Snakes'){
         $('#dependent').select2('destroy').empty().select2({data: snakes});
    }

});

here is a fiddle 这是一个小提琴

https://jsfiddle.net/rLmztr2d/2462/ https://jsfiddle.net/rLmztr2d/2462/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM