如何禁用selt2中的标题 [英] How to disable the title in select2

查看:20
本文介绍了如何禁用selt2中的标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下所示的selt2下拉列表:

    $(function () {
  $("#itemSelect").select2().on("select2:select", function (e) {
   $("#itemSelect").val(-1).trigger("change");
        var id = e.params.data.title;
        var url = siteRoot + "/site/item?itemID=" + id ;
        $("#Container").load(url);
    });
});

它从我的模型中获取其值,格式为html:

<select class="js-data-example-ajax" aria-expanded="true" style="width: 100%; display: none;" id="itemSelect">
<option disabled selected value="-1"> Search by item </option>
@foreach (var item in Model)
{
    <option text="@item.Id" title="@item.Id">
        item.Name
    </option>
}

一切工作正常,除了当我选择一个项目并加载它时,我可以将鼠标悬停在下拉列表上,它会显示该项目的ID。我不想显示ID!

在图片中,您可以看到当我将鼠标悬停在"冰茶"上时出现的下拉列表和商品编号

我知道这是因为selt2通过var id = e.params.data.title;获取id,但我如何更改这一点? 它不能与var id = e.params.data.id;

一起使用

我尝试使用工具提示,但我对此还不熟悉。

//$("#select2-itemSelect-container").tooltip({
//    title: "Search item",

//    placement: "auto"
//});

我只想在悬停时删除下拉列表中的ID。 感谢您的每一份帮助。

推荐答案

我可能会晚一点,但这些解决方案对我都不起作用,因为我是在向UI动态添加选定的2个字段。

此代码为我解决了问题:

$('.select2-selection__rendered').hover(function () {
    $(this).removeAttr('title');
});

如果您还在动态添加selt2字段,请不要忘记始终在上面的代码之前执行此代码:

$('.select2-selection__rendered').unbind('mouseenter mouseleave');

此代码将首先删除所有选定2字段上的on hover侦听器。

这篇关于如何禁用selt2中的标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆