Jquery select2 ajax 链式选择

标签 jquery jquery-select2

要求: <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>

//JSON result from ajax.php (if no results, false is returned) [{"id":"1","make":"Foton"},{"id":"4","make":"Hyundai"},{"id":"5","make":"KIA"},{"id":"2","make":"Proton"},{"id":"2","make":"Proton"},{"id":"3","make":"Tata"},{"id":"3","make":"Tata"},{"id":"6","make":"Toyota"}]

最佳答案

尝试升级插件,

我尝试使用 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'});


            }

关于Jquery select2 ajax 链式选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17083661/

相关文章:

javascript - 更新文本后输入的值

javascript - 如何根据屏幕尺寸调整图像大小

javascript - 取消选择 Select2 jquery 插件中的选择选项

javascript - kongregate未定义错误javascript

javascript - 动态添加 <Img> 标签

jquery - yii2 kartik select2 插件在模式下不起作用

javascript - JQuery select2 e.val 更改时未定义

twitter-bootstrap - yii select2 插件预加载数据

Javascript 函数导致 HTML 页面重新加载 : why?

jquery - 如何隐藏(而不是删除)select2中的选项?