简体   繁体   English

如何禁用select2中的标题

[英]How to disable the title in select2

I've a select2 dropdown which looks like this :我有一个 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);
    });
});

It gets it's values from my Model in html:它从我的 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>
}

Everything works fine, EXCEPT when I choose an item and it's loaded, I can hover over the dropdown and it shows me the ID from the item.一切正常,除了当我选择一个项目并加载它时,我可以将鼠标悬停在下拉菜单上,它会显示该项目的 ID。 I don't want to show the ID!我不想出示身份证!

在此处输入图片说明

In the picture you see the dropdown and the item number which appears when I hover over "Ice Tea"在图片中,您会看到当我将鼠标悬停在“冰茶”上时出现的下拉菜单和项目编号

I know it's because select2 gets the id by var id = e.params.data.title;我知道这是因为 select2 通过var id = e.params.data.title;获取 id var id = e.params.data.title; , but how can I change this? ,但我该如何改变呢? It's not working with var id = e.params.data.id;它不适用于var id = e.params.data.id;

I tried to use tooltip, but I'm new to this.我尝试使用工具提示,但我对此很陌生。

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

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

I just want to get rid of the ID in the dropdown while hover over.我只想在悬停时摆脱下拉列表中的 ID。 Every help is appreciated.每一个帮助表示赞赏。

I might be a bit late, but non of this solutions worked for me since I was dynamically adding select2 fields to UI.我可能有点晚了,但是由于我正在向 UI 动态添加 select2 字段,因此这些解决方案都不适合我。

This code did the trick for me:这段代码对我有用:

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

If you are also dynamically adding select2 fields don't forget always to execute this code before above one:如果您还动态添加 select2 字段,请不要忘记始终在上述代码之前执行此代码:

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

This code will first remove on hover listener on all select2 fields.此代码将首先删除所有 select2 字段on hover侦听器。

The problem can be reproduced in Select2 v4 by placing the mouse over the select box (in single selection mode) or over the selected tags (in multiple selection mode):Select2 v4 中,可以通过将鼠标放在选择框上(单选模式)或所选标签上(多选模式)来重现该问题:

在此处输入图片说明 在此处输入图片说明

The plugin attaches a title attribute to these elements by default, and there is no configuration option available to disable this behaviour.默认情况下,插件将title属性附加到这些元素,并且没有可用于禁用此行为的配置选项。

I had ended up writing a small extension for the Select2 plugin.我最终为Select2插件编写了一个小扩展。 I've added a new option, selectionTitleAttribute , which must be set to false to remove the title attribute.我添加了一个新选项selectionTitleAttribute ,它必须设置为 false 才能删除title属性。

Add the following code right after the plugin's js file:在插件的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);

Usage用法

Initialize the select2 plugin with the selectionTitleAttribute option set to false :selectionTitleAttribute选项设置为false来初始化 select2 插件:

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

Demo演示

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

You can use events to remove title tag.您可以使用事件来删除标题标签。

For example, this code works with select2 v4.0:例如,此代码适用于 select2 v4.0:

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

http://jsfiddle.net/by8k1dv9/ http://jsfiddle.net/by8k1dv9/

hey everyone if you have this problem this simple trick嘿大家如果你有这个问题这个简单的技巧

<script>

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


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

});

Try disabling the tooltip for the created select2.尝试禁用创建的 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');
});

After initializing select2, use below line of code to remove title attribute from select2 options from code.初始化 select2 后,使用下面的代码行从代码中的 select2 选项中删除 title 属性。 remove ` sign then put in your script file删除 ` 符号然后放入您的脚本文件

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

Unfortuntely I didn't have any luck with the above solutions, possibly because this project uses bootstrap and Select2 was utilising bootstrap tooltips, which look different and fade in.不幸的是,我对上述解决方案没有任何运气,可能是因为该项目使用引导程序,而 Select2 正在使用引导程序工具提示,它们看起来不同并且淡入。

In this case, upon mouseover, Select2 would actually remove the title attribute, replacing it with aria-describedby which contained an id of a newly-created toolip element.在这种情况下,在鼠标悬停时,Select2 实际上会删除title属性,用包含新创建的工具栏元素的 id 的aria-describedby替换它。 On mouseleave, aria-describedby was removed and the title attribute restored again.在 mouseleave 时, aria-describedby descriptionby 被删除并再次恢复了title属性。

So trying to remove the title attribute didn't work, as it had already been removed, and then it was restored again on mouseleave.所以试图删除title属性没有用,因为它已经被删除了,然后在 mouseleave 上又恢复了。 So this did the trick for me:所以这对我有用:

$(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
});

The above code doesn't prevent Select2 from attempting to show a tooltip the first time the mouse hovers over it, but .hide() successfully prevents it being shown before it starts to fade in. After that, unbinding the hover stops Select2 from trying to show any more of them.上面的代码不会阻止 Select2 在鼠标一次悬停在它上面时尝试显示工具提示,但是.hide()成功地阻止了它在开始淡入之前显示。之后,解除悬停会阻止 Select2 尝试显示更多。

I trying use this, and it's works :我尝试使用它,它的工作原理:

        $(document).on('mouseenter', '.select2-selection__rendered', function () {
            $('.select2-selection__rendered').removeAttr('title');
        });

You can simply comment these couple of lines in your select2 script您可以简单地在 select2 脚本中注释这几行

//$rendered.attr('title', selection.title || selection.text);
//$selection.attr('title', selection.title || selection.text);

This won't add the title attribute at the first place这不会首先添加 title 属性

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM