【问题标题】:Materialize select not working append option with物化选择不工作的附加选项
【发布时间】:2018-02-13 08:28:26
【问题描述】:

我在我的项目中使用materialize css,当我想在matrialize select 中使用ajax 附加选项时遇到问题。我已经关注了这个答案How to dynamically modify <select> in materialize css framework。但不工作。

在我的 html 中,我使用物化模式来渲染物化表单。这是我的代码:

                   <div class="input-field col s12">                            
                        <select id="select_1" name="select_1">
                            <option value="1">option 1</option>
                            <option value="2">option 2Barat</option>     
                        </select>
                      <label for="select_1">Select 1</label>
                    </div>

                     <div class="input-field col s12">                          
                        <select id="select_2"  name="select_2">
                            <option value="0" disabled="disabled">Choose option</option>
                        </select>
                      <label for="select_2">Select 2</label>
                    </div>

我的 js

<script type="text/javascript">
    $(document).ready(function(){
        function get_selected(){
            var base_url = '<?php echo base_url() ?>';
            var data = $('#select_1').val();
            $.ajax({
                url: ""+base_url+"admin/get_data/",
                dataType: 'json',
                type: "POST",
                data:{'data':data},
                beforeSend: function() {

                },                  
                success: function (data) {
                    var text = '';
                    var $selectDropdown = $("#select_1").empty().html(' ');

                    $.each(data, function(i, val){                      
                        $selectDropdown.append($("<option></option>").attr("value",val.id).text(val.name));

                    })

                    $selectDropdown.trigger('contentChanged');
                    $('select').on('contentChanged', function() {
                      // re-initialize (update)
                      $(this).material_select();
                    });
                },
                error: function (xhr, status, err) {
                    // console.log(xhr);
                    // console.log(status);
                    // console.log(err);
                },
            });
        }


        $(document).on('change','#select_1', function(){
            get_selected();         
        })
    })
</script>

注意:我使用 jquery 3.3.1 并实现版本 0.100.2。谢谢你的帮助,对不起我的英语:)

【问题讨论】:

    标签: javascript jquery html css materialize


    【解决方案1】:

     $(document).ready(function() {
        $('select').material_select();
     });
     
     $(document).on('change','#select_1', function(){
         get_selected();         
    })
    
    
    function get_selected(){
         var data=[{id:1,name:"ABC"},{id:2,name:"XYZ"},{id:3,name:"PQR"}];
         
         var Options="";
        $.each(data, function(i, val){ 
          Options=Options+"<option value='"+val.id+"'>"+val.name+"</option>";
      });
      $('#select_2').empty();
      $('#select_2').append(Options);
      $("#select_2").material_select()
    } 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    
    
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/css/materialize.min.css">
    
    
      <script src="https://cdnjs.cloudflare.com/ajax/libs/materialize/0.100.2/js/materialize.min.js"></script>
        
    <div class="row">    
    <div class="input-field col s12">                            
    <select id="select_1" name="select_1">
    <option value="1">option 1</option>
    <option value="2">option 2Barat</option>     
    </select>
    <label for="select_1">Select 1</label>
    </div>
                        
    <div class="input-field col s12">                          
    <select id="select_2"  name="select_2">
    <option value="0" disabled="disabled">Choose option</option>
    </select>
    <label for="select_2">Select 2</label>
    </div>
    </div>

    【讨论】:

    • 这不是答案
    【解决方案2】:

    对于 2019 年之后来到这里的任何人,请注意,API 已更改,material_Select() 不再有效。 使用,

        $('#my_Element').append(<Array of options>)
        $('#my_Element').formSelect()
    

    【讨论】:

    • 感谢工作:D 你有这方面的背景吗?为什么要使用formSelect(),它是干什么的?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-13
    • 1970-01-01
    • 2011-10-24
    相关资源
    最近更新 更多