如何在php的引导multi-select中使用ajax获取依赖数据?

如何在php的引导multi-select中使用ajax获取依赖数据?,php,jquery,ajax,bootstrap-4,multi-select,Php,Jquery,Ajax,Bootstrap 4,Multi Select,如何使用bootsrap multi-select创建多选从属下拉列表 如果选择“州”,请在“城市”框中填充选定的州城市 下面我提到我的ajax代码数据将被获取,但不会填入城市框 ajax代码 您认为您检查了值stateId。我的示例: <select id="my_multi_select1"> <option value="1">test1</option> <option value="2" selected="selected"&

如何使用bootsrap multi-select创建多选从属下拉列表

如果选择“州”,请在“城市”框中填充选定的州城市

下面我提到我的ajax代码数据将被获取,但不会填入城市框

ajax代码
您认为您检查了值stateId。我的示例:

<select id="my_multi_select1">
    <option value="1">test1</option>
    <option value="2" selected="selected">test2</option>
    <option value="3">test3</option>
</select>
<script>
    var e = document.getElementById("my_multi_select1");
    var stateId  = e.options[e.selectedIndex].value; // GET VALUE DROP DOWN
</script>
完整的CakePHP答案 首先将JS和CSS放在您的cakeproject中

CSS 1.bootstrap-multiselect.css

2.multi-select.css

JS bootstrap-select.min.js

jquery.multi-select.js

查看文件代码 可变建议: $state=>从控制器获取所有状态

$selected_state=>添加数据后,它将用于数据库中的选定状态数据

为多选择框添加脚本 用于多选数据的AJAX 所有代码都放在视图文件中,并通过多个状态id获取城市,城市与状态id串联,如2_56表示单个foreach


谢谢

请发布一些您尝试过的代码,并提及正在解决的具体问题faced@NitinSinghajax代码加载到我的question@NitinSingh我找到了这个解决方案。。。。Thanks@Chintan你试着用这个关键词
<select id="my_multi_select1">
    <option value="1">test1</option>
    <option value="2" selected="selected">test2</option>
    <option value="3">test3</option>
</select>
<script>
    var e = document.getElementById("my_multi_select1");
    var stateId  = e.options[e.selectedIndex].value; // GET VALUE DROP DOWN
</script>
<?php echo $this->Form->input("state_id", array('type' => 'select', 'options' => array($state), 'default' => $selected_state, 'id'=>'my_multi_select1', 'multiple'=>'multiple', 'class' => 'multi-select', 'label' => FALSE_VALUE,'div' => FALSE_VALUE)); ?>

<?php echo $this->Form->input("city_id", array('type' => 'select', 'options' => '', 'default' => '', 'id'=>'my_multi_select2', 'multiple'=>'multiple', 'class' => 'multi-select', 'label' => FALSE_VALUE,'div' => FALSE_VALUE)); ?>
<script>
$('#my_multi_select1').multiSelect();
$('#my_multi_select2').multiSelect();
</script>
<script type="text/javascript">
  $(document).ready(function() {
   $("#my_multi_select1").change(function () {
            var stateId = $("#my_multi_select1").val();
            $.ajax({
                type: 'POST',
                url: 'http://loalhost/project/admin/City/getMultiCitylist/',
                data:{state_id:stateId},
                beforeSend: function(){

                },
                error : function (xhr, textStatus, errorThrown) {

                },
                complete : function (result){

                   //$selected_city => from controller after add data(this ajax for oth add and edit opration)

                    <?php if(isset($selected_city) && !empty($selected_city)) { ?>

                    $('#my_multi_select2').attr('disabled', false);


                    $("#my_multi_select2").empty();


                    var prePopulate = JSON.parse(result.responseText);

                    $.each(prePopulate, function (i, city) {
                      var new_id = i.split('_');
                      $("#my_multi_select2").append('<option value="' + i + '">' +
                            territory + '</option>');
                       });



                     <?php foreach ($selected_city as $key => $value) {
                        ?>
                      $("#my_multi_select2 option[value='<?php echo $value ?>']").prop('selected', 'selected').trigger('change');
                      <?php } ?>

                    $('#my_multi_select2').multiselect('refresh');
                    <?php } ?>
                },
                success: function (result) {  
                   $("#my_multi_select2").empty();                       


                    var prePopulate = JSON.parse(result);

                    $.each(prePopulate, function (i, territory) 
                        $("#my_multi_select2").append('<option value="' + i + '">' +
                            territory + '</option>');
                    });
                    $('#my_multi_select2').multiselect('refresh');


                  }

            });
        });       
});
// this ajax for trigger added data on edit mode
 <?php if(isset($selected_city) && count($selected_city) > 0) { ?>
<script>

   $(document).ready(function(){
      $.ajax({
         type: "POST",
         url: 'http://localhost/Project/admin/City/getMultiCitylist',
         data: $("#my_multi_select2").closest("form").serialize(),
         success: function(result) {
            $("#my_multi_select2").empty();
                   $("#my_multi_select2").empty();


                    var prePopulate = JSON.parse(result);

                    $.each(prePopulate, function (i, city) {
                        $("#my_multi_select2").append('<option value="' + i + '">' +
                            city + '</option>');
                    });

                    $('#my_multi_select2').multiselect('refresh');
        },
        beforeSend: function(){
            $('#my_multi_select2').attr('disabled', true);
        },
        error : function (xhr, textStatus, errorThrown) {
            //other stuff
        },
        complete : function (result){ 
                    $('#my_multi_select2').attr('disabled', false);


                    $("#my_multi_select2").empty();


                    var prePopulate = JSON.parse(result.responseText);

                    $.each(prePopulate, function (i, city) {
                      var new_id = i.split('_');
                      $("#my_multi_select2").append('<option value="' + i + '">' +
                            city + '</option>');
                       });



                     <?php foreach ($selected_city as $key => $value) {
                        ?>
                      $("#my_multi_select2 option[value='<?php echo $value ?>']").prop('selected', 'selected').trigger('change');
                      <?php } ?>

                    $('#my_multi_select2').multiselect('refresh');


        }
    });

 });

  </script>
  <?php } ?>