jQuery Select2子项未显示为选中状态

Anoop公关

我在数据中有一个带有“ children”属性的select2框,它按预期方式呈现。但是,在编辑屏幕中,当我们选择子元素时,其父元素会保留在下拉列表中。例如,当我们在添加表单中选择“ Live Band”时,但编辑表单显示为“ Entertainment”。请找到我正在使用的数据格式和源代码。请帮助我解决此问题。

数据

[
    {
        "id": "12",
        "text": "Personal Chef",
        "parent": "0",
        "order": 0
    },
    {
        "id": "11",
        "text": "Entertainment",
        "parent": "0",
        "order": 1,
        "children": [
            {
                "id": "37",
                "text": "DJ’s",
                "parent": "11",
                "order": 0
            },
            {
                "id": "38",
                "text": "Live Band",
                "parent": "11",
                "order": 1
            },
            {
                "id": "36",
                "text": "Clowns - Kids parties",
                "parent": "11",
                "order": 2
            }
        ]
    },
    {
        "id": "10",
        "text": "Motorcycle Repair",
        "parent": "0",
        "order": 2
    }
]

源代码:

$(".search-result-box").select2({
    placeholder: "eg: 1967 Stingray Mechanic",
    multiple: false,
    width: 225,
    selectOnBlur: true,
    data: data
});

$(".search-result-box").select2('val', 38); // Tried select2('val', "38") also.
奇查尔·尼尔

我只是在plunker中尝试了您的select2示例,它按照您的期望工作。

请看一下,如果您还有其他需要,请告诉我。还请提及您的jquery,以及select2版本。

HTML:

<!DOCTYPE html>
<html>

<head>
  <script data-require="[email protected]" data-semver="1.7.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
  <link data-require="select2@*" data-semver="3.5.1" rel="stylesheet" href="//cdn.jsdelivr.net/select2/3.4.5/select2.css" />
  <script data-require="select2@*" data-semver="3.5.1" src="//cdn.jsdelivr.net/select2/3.5.1/select2.min.js"></script>

  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body>

  <input type="hidden" class="search-result-box" />

</body>

</html>

JavaScript文件:

$(function() {

   $(".search-result-box").select2({
     placeholder: "eg: 1967 Stingray Mechanic",
     multiple: false,
     width: 225,
     selectOnBlur: true,
     data: data
   });

  $(".search-result-box").select2('val', 38); // Tried select2('val', "38") also.

});

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

在select2 jquery插件中未调用initSelection

来自分类Dev

jQuery Select 2未显示所选值

来自分类Dev

jQuery select2在选择时显示optgroup标签

来自分类Dev

使用jQuery更新状态后,复选框未显示已选中的i UI

来自分类Dev

单选按钮未显示为选中状态

来自分类Dev

单选按钮未显示为选中状态

来自分类Dev

设置复选框为jQuery选中状态

来自分类Dev

jQuery select2设置输入值

来自分类Dev

使用Ajax的jQuery Select2插件

来自分类Dev

jQuery Select2 Android隐藏键盘

来自分类Dev

jQuery Select2克隆

来自分类Dev

jQuery Select2数据属性

来自分类Dev

以Jquery select2形式发布

来自分类Dev

jQuery select2刷新数据

来自分类Dev

JQuery select2与Frontaccounting集成

来自分类Dev

jQuery Select2 4.0 Bootstrap样式

来自分类Dev

XPages中的Select2 JQuery插件

来自分类Dev

jQuery Select2插件+ Laravel 5.3

来自分类Dev

jQuery Select2 JSON 数据

来自分类Dev

当未选中单选按钮时,如何使jQuery警报显示?

来自分类Dev

使用 jQuery,为 Select2 选择下拉列表赋值不起作用

来自分类Dev

如何检查选择列表是否为多个?Select2 JQuery、JavaScript

来自分类Dev

显示列表而不绑定在select2 ajax jQuery中

来自分类Dev

如何在 jQuery select2 中显示 optgroup 标签+值

来自分类Dev

如何在ajax jquery中使动态表中的动态单选按钮处于选中或未选中状态

来自分类Dev

select2 json将所有选项显示为.text()而不是被选中

来自分类Dev

使用jQuery和两个selects,select2会显示禁用的选项,其值比select1低?

来自分类Dev

Select2 下拉列表相对于 select2 的显示值向左偏移 -5px。不能用 jquery 改变

来自分类Dev

select2 占位符显示组文本和子项

Related 相关文章

热门标签

归档