【问题标题】:Jquery select2 ajax chained selectsJquery select2 ajax 链式选择
【发布时间】:2013-06-09 15:17:59
【问题描述】:

要求: <select1> 包含一些选项(占位符文本 = “选择新的或二手的车辆”) <select2> 禁用且没有选项(占位符文本 = “选择制造商”)

<select1> 用户选择一个选项 <select2> 填充来自 1 的结果

利用 Jquery Select2 插件

我已经让链式选择正常工作,正确填充 select2,还使用了 select2 插件

问题: 我希望占位符文本在得到结果时显示“找到 5 个结果” 我希望占位符文本在没有结果时说“找到 0 个结果”并返回禁用 目前,占位符文本在第一次选择时会发生变化,重新选择时会混淆 select2 占位符

HTML:

//<select1>
<select name='category_id' id='category_id'>
<option value='1'>New</option>
<option value='2'>Used</option>
</select>

//<select2>
<select name='make_id' id='make_id'><option value=''></option></select>

//javascript
<script type='text/javascript'>
    $(document).ready(function() {
        $('#category_id').select2({
            placeholder: 'Select new or used Vehicles',
            allowClear: true
        });
        $('#make_id').select2({
            placeholder: 'Select Manufacturer',
            allowClear: true
        });

        $('#category_id').change(function() {
            var selectedCategory = $('#category_id option:selected').val();
            $.ajax({
                type: 'POST',
                url: 'ajax.php',
                dataType: 'html',
                data: {
                    a: 'dropDownsHome',
                    c: selectedCategory
                },
                success: function(txt){
                    //no action on success, its done in the next part
                }
            }).done(function(data){
                //get JSON
                data = $.parseJSON(data);
                //generate <options from JSON
                var list_html = '';
                $.each(data, function(i, item) {
                    list_html += '<option value='+data[i].id+'>'+data[i].make+'</option>';
                });
                //replace <select2 with new options
                $('#make_id').html(list_html);
                //set to enabled|disabled
                if (data.length > 1) {
                    $('#make_id').select2('enable', true); //enable form
                }else{
                    $('#make_id').select2('enable', false); //disable form
                }
                //change placeholder text
                $('#make_id').select2({placeholder: data.length +' results'});
            })
        });
    });
</script>

//来自ajax.php的JSON结果(如果没有结果,则返回false) [{"id":"1","make":"Foton"},{"id":"4","make":"现代"},{"id":"5","make": "KIA"},{"id":"2","make":"Proton"},{"id":"2","make":"Proton"},{"id":"3", "make":"Tata"},{"id":"3","make":"Tata"},{"id":"6","make":"Toyota"}]

【问题讨论】:

    标签: jquery jquery-select2


    【解决方案1】:

    尝试升级插件,

    我尝试使用 3.4.0 版本启用和禁用工作正常,还添加了

    list_html += ' <option value=""></option>';
    

    上面的代码和代码一起显示,然后占位符也会显示..我已经在下面粘贴了编辑后的代码

    //generate <options from JSON
                var list_html = '';
                list_html += ' <option value=""></option>';
    
                $.each(data, function(i, item) {
                    list_html += '<option value='+data[i].id+'>'+data[i].make+'</option>';
                });
                //replace <select2 with new options
                $('#make_id').html(list_html);
                //set to enabled|disabled
                if (data.length > 1) {
                    $('#make_id').select2('enable', true); //enable form
                    $('#make_id').select2({placeholder: data.length +' results'});
    
                }else{
    
                    $('#make_id').select2('enable', false); //disable form
                    $('#make_id').select2({placeholder: 0 +' results'});
    
    
                }
    

    【讨论】:

    • 我现在试试这个谢谢.. 我正在使用版本:/* 版权所有 2012 Igor Vaynberg 版本:3.4.0 时间戳:2013 年 5 月 14 日星期二 08:27:33 PDT
    • 成功了,谢谢!我仍然不明白为什么它会起作用,但至少我可以继续这个项目......在这样的小事情上已经花了很多时间。发送!!
    • +1 解决了我的链接国家/州 select2 的问题。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-30
    相关资源
    最近更新 更多