I have a <select> list that I'm styling with select2.js. For the most part everything works, however, when a placeholder is used, a user drops down the list, and then tries to select the first item in the list they are not able to. If the user selects any item other than the first item and then goes back and selects the first item that works fine. It's as if the first item is already selected (it is highlighted already) but not really (since the value of the list is empty and the placeholder is displayed).

我有一个

HTML

    <select id="selectBox"></select>

Binding JS

$("#selectBox").select2({
    placeholder: "Select an item",
    width: '267px' });

Populate JS

var showThings = function() {

    var $selectBox = $('#selectBox');

    $selectBox.empty();

    blah.executeRequest('getThings', function(result) {

            $(result.sort(SortByName)).each(function(index, user) {

                var asOption = [];

                asOption.push('<option value ="' + user['id'] + '">' +
                    user['name'] + '</option>');


                if (asOption.length > 0) {
                    $selectBox.append(asOption.join('\n'));
                }
            });
        }
    );
};

The "executeRequest" is just grabbing data from some Java code, populating the list works fine, but figured I'd include the code so that it could be seen where the list data is coming from.

“executeRequest”只是从一些Java代码中获取数据,填充列表工作正常,但我想包含代码,以便可以看到列表数据的来源。

If anyone has any ideas on this, or has experienced it and knows how to fix it, I'd be most appreciative.

如果有人对此有任何想法,或经历过它并知道如何解决它,我将非常感激。

Thanks,

Chris

1 个解决方案

#1


0

Before pushing "real" values, try adding an empty <option></option> to the beginning of your select element.

在推送“实际”值之前,请尝试将空

Update: I wrote a JSFiddle example for you.

更新:我为您编写了一个JSFiddle示例。

更多相关文章

  1. 当使用Javascript选择其中一个时,如何禁用复选框列表中的其他项目
  2. 笨鸟求问,下拉列表实现局部刷新的问题
  3. 将JavaScript数组转换成逗号分隔列表的简单方法?
  4. 使用Ajax+JQuery构造分页查询列表
  5. 如何使用jQuery选择列表中的最后X项?
  6. 使用jquery tablesorter插件,有一个选中项目的下拉框
  7. jquery.min.js引入项目报错
  8. 基于jQuery实现页面搜索功能----项目实战
  9. 单击列表项时如何编写内容? [重复]

随机推荐

  1. Android更新Ui进阶精解(二)
  2. Android更新Ui进阶精解(一)
  3. Android记事本NotePad应用功能拓展(四)
  4. 元素可指定如下常用属性
  5. android 兼容所有刘海屏的方案大全
  6. Android UI Design
  7. Android 资料总结(全)
  8. android 的一些小知识
  9. android菜鸟日记1
  10. 【安卓学习之开发工具】 Android Studio