11

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

    $(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。我不想出示身份证!

在此处输入图像描述

在图片中,当我将鼠标悬停在“冰茶”上时,您会看到下拉列表和项目编号

我知道这是因为 select2 获取 id var id = e.params.data.title;,但是我该如何更改呢?它不适用于 var id = e.params.data.id;

我尝试使用工具提示,但我对此并不陌生。

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

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

我只想在悬停时摆脱下拉列表中的 ID。感谢您的每一次帮助。

4

8 回答 8

7

我可能有点晚了,但是由于我正在向 UI 动态添加 select2 字段,因此没有一个解决方案对我有用。

这段代码对我有用:

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

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

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

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

于 2018-03-13T11:33:02.030 回答
6

通过将鼠标放在选择框(单选模式)或选定标签(多选模式)上,可以在Select2 v4中重现该问题:

在此处输入图像描述 在此处输入图像描述

该插件默认为这些元素附加一个title属性,并且没有可用于禁用此行为的配置选项。

我最终为Select2插件编写了一个小扩展。我添加了一个新选项 ,selectionTitleAttribute必须将其设置为 false 才能删除该title属性。

在插件文件之后添加以下代码js

;(function($) {

  // Extend defaults
  //
  var Defaults = $.fn.select2.amd.require('select2/defaults');

  $.extend(Defaults.defaults, {
    selectionTitleAttribute: true
  });

  // SingleSelection
  //
  var SingleSelection = $.fn.select2.amd.require('select2/selection/single');

  var _updateSingleSelection = SingleSelection.prototype.update;

  SingleSelection.prototype.update = function(data) {

    // invoke parent method
    _updateSingleSelection.apply(this, Array.prototype.slice.apply(arguments));

    var selectionTitleAttribute = this.options.get('selectionTitleAttribute');

    if (selectionTitleAttribute === false) {
      var $rendered = this.$selection.find('.select2-selection__rendered');
      $rendered.removeAttr('title');
    }

  };


  // MultipleSelection
  //
  var MultipleSelection = $.fn.select2.amd.require('select2/selection/multiple');

  var _updateMultipleSelection = MultipleSelection.prototype.update;

  MultipleSelection.prototype.update = function(data) {

    // invoke parent method
    _updateMultipleSelection.apply(this, Array.prototype.slice.apply(arguments));

    var selectionTitleAttribute = this.options.get('selectionTitleAttribute');

    if (selectionTitleAttribute === false) {
      var $rendered = this.$selection.find('.select2-selection__rendered');
      $rendered.find('.select2-selection__choice').removeAttr('title');
      this.$selection.find('.select2-selection__choice__remove').removeAttr('title');
    }

  };

})(window.jQuery);

用法

selectionTitleAttribute使用设置为的选项初始化 select2 插件false

$("select").select2({
  // other options 
  selectionTitleAttribute: false
});

演示

小提琴:http: //jsfiddle.net/hr8bqnpn/

于 2017-09-20T16:24:16.083 回答
3

您可以使用事件来删除标题标签。

例如,此代码适用于 select2 v4.0:

$('select').on('change', function (evt) {
    $('.select2-selection__rendered').removeAttr('title');
});

http://jsfiddle.net/by8k1dv9/

于 2017-01-26T00:57:56.143 回答
1

嘿大家如果你有这个问题这个简单的技巧

<script>

$(document).on('mouseenter', '.select2-selection__rendered',  function () {


    $(this).removeAttr("title");

});
于 2020-10-06T13:26:51.107 回答
1

初始化 select2 后,使用下面的代码行从代码中的 select2 选项中删除 title 属性。删除`符号然后放入你的脚本文件

   $("ul.select2-selection__rendered").hover(function(){
      $(this).find('li').removeAttr('title');
    });
于 2019-02-23T05:20:45.520 回答
0

不幸的是,我对上述解决方案没有任何运气,可能是因为该项目使用引导程序,而 Select2 正在使用引导工具提示,它们看起来不同并且淡入。

在这种情况下,当鼠标悬停时,Select2 实际上会删除该title属性,将其替换为aria-describedby包含新创建的 toollip 元素的 id 的属性。在 mouseleave 时,aria-describedby被删除并title再次恢复属性。

所以试图删除该title属性是行不通的,因为它已经被删除了,然后在 mouseleave 上再次恢复。所以这对我有用:

$(document).on('mouseenter', '.select2-selection__rendered', function () {
  var tooltipId = $(this).attr("aria-describedby"); // contains the id of the tooltip
  $("#" + tooltipId).hide(); // hide the tooltip before it fades in
  $(this).unbind('mouseenter mouseleave'); // stop it showing another tooltip
});

上面的代码不会阻止 Select2在鼠标一次悬停在它上面时尝试显示工具提示,但.hide()成功地阻止它在它开始淡入之前显示。之后,取消绑定悬停会阻止 Select2 尝试再显示其中。

于 2020-07-31T07:43:49.547 回答
0

我尝试使用它,它的工作原理:

        $(document).on('mouseenter', '.select2-selection__rendered', function () {
            $('.select2-selection__rendered').removeAttr('title');
        });
于 2021-05-15T06:43:15.780 回答
0

尝试禁用创建的 select2 的工具提示。

$(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);
    }).tooltip('disable');
});
于 2016-02-19T08:51:51.543 回答