admin管理员组

文章数量:1289866

I have a select2 dropdown in my JSP page and its options. I want to add a title to that select2 dropdown whenever its option is changed. I tried adding title to it. But when I hover over the select2 box, the title doesn't appear. But however the title attribute is appended to the select2 drop down. Help me solve this please.

<select id="daySelect" title="select your day">
 <option val="0">Sunday</option>
 <option val="1">Monday</option>
 <option val="2">Tuesday</option>
 <option val="3">Wednesday</option>
 <option val="4">Thursday</option>
 <option val="5">Friday</option>
 <option val="6">Saturday</option>
</select>

I have a select2 dropdown in my JSP page and its options. I want to add a title to that select2 dropdown whenever its option is changed. I tried adding title to it. But when I hover over the select2 box, the title doesn't appear. But however the title attribute is appended to the select2 drop down. Help me solve this please.

<select id="daySelect" title="select your day">
 <option val="0">Sunday</option>
 <option val="1">Monday</option>
 <option val="2">Tuesday</option>
 <option val="3">Wednesday</option>
 <option val="4">Thursday</option>
 <option val="5">Friday</option>
 <option val="6">Saturday</option>
</select>
Share Improve this question edited Sep 4, 2017 at 8:39 Granny 79311 silver badges22 bronze badges asked Sep 4, 2017 at 6:59 SelluboySelluboy 691 gold badge1 silver badge5 bronze badges 1
  • It is possible that you have a CSS or JS code that handles the select dropdown hover function and could be interfering? – thxou Commented Sep 4, 2017 at 7:05
Add a ment  | 

8 Answers 8

Reset to default 1

the title are not being copied to span used to show the options in select2 version 3.5.*

However, you could do change the select2.js as follows:-

populateResults: function(container, results, query) {
                var populate, id=this.opts.id, liveRegion=this.liveRegion;

                populate=function(results, container, depth) {

                    var i, l, result, selectable, disabled, pound, node, label, innerContainer, formatted;

                    results = opts.sortResults(results, container, query);

                    // collect the created nodes for bulk append
                    var nodes = [];
                    for (i = 0, l = results.length; i < l; i = i + 1) {

                        result=results[i];

                        disabled = (result.disabled === true);
                        selectable = (!disabled) && (id(result) !== undefined);

                        pound=result.children && result.children.length > 0;

                        node=$("<li></li>");
                        node.addClass("select2-results-dept-"+depth);
                        node.addClass("select2-result");
                        node.addClass(selectable ? "select2-result-selectable" : "select2-result-unselectable");
                        if (disabled) { node.addClass("select2-disabled"); }
                        if (pound) { node.addClass("select2-result-with-children"); }
                        node.addClass(self.opts.formatResultCssClass(result));
                        node.attr("role", "presentation");

                        label=$(document.createElement("div"));
                        label.addClass("select2-result-label");
                        label.attr("id", "select2-result-label-" + nextUid());
                        label.attr("role", "option");
                        if(result.element.length > 0 && result.element[0].title != undefined)
                            label.attr("title", result.element[0].title || '');

                        formatted=opts.formatResult(result, label, query, self.opts.escapeMarkup);
                        if (formatted!==undefined) {
                            label.html(formatted);
                            node.append(label);
                        }


                        if (pound) {
                            innerContainer=$("<ul></ul>");
                            innerContainer.addClass("select2-result-sub");
                            populate(result.children, innerContainer, depth+1);
                            node.append(innerContainer);
                        }

                        node.data("select2-data", result);
                        nodes.push(node[0]);
                    }

                    // bulk append the created nodes
                    container.append(nodes);
                    liveRegion.text(opts.formatMatches(results.length));
                };

                populate(results, container, 0);
            }

This single line change is just what we need:-

 if(result.element != undefined && result.element.length > 0 && result.element[0].title != undefined)
                            label.attr("title", result.element[0].title || '');

Check this it might help you!

$('#selectb').select2({
    placeholder: 'Please select ...',
    allowClear: true
}).val(null).trigger('change');
 $( document ).tooltip();
.select2-container {
  box-sizing: border-box;
  display: inline-block;
  margin: 0;
  position: relative;
  vertical-align: middle; }
  .select2-container .select2-selection--single {
    box-sizing: border-box;
    cursor: pointer;
    display: block;
    height: 28px;
    user-select: none;
    -webkit-user-select: none; }
    .select2-container .select2-selection--single .select2-selection__rendered {
      display: block;
      padding-left: 8px;
      padding-right: 20px;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap; }
  .select2-container[dir="rtl"] .select2-selection--single .select2-selection__rendered {
    padding-right: 8px;
    padding-left: 20px; }
  .select2-container .select2-selection--multiple {
    box-sizing: border-box;
    cursor: pointer;
    display: block;
    min-height: 32px;
    user-select: none;
    -webkit-user-select: none; }
    .select2-container .select2-selection--multiple .select2-selection__rendered {
      display: inline-block;
      overflow: hidden;
      padding-left: 8px;
      text-overflow: ellipsis;
      white-space: nowrap; }
  .select2-container .select2-search--inline {
    float: left; }
    .select2-container .select2-search--inline .select2-search__field {
      box-sizing: border-box;
      border: none;
      font-size: 100%;
      margin-top: 5px; }
      .select2-container .select2-search--inline .select2-search__field::-webkit-search-cancel-button {
        -webkit-appearance: none; }

.select2-dropdown {
  background-color: white;
  border: 1px solid #aaa;
  border-radius: 4px;
  box-sizing: border-box;
  display: block;
  position: absolute;
  left: -100000px;
  width: 100%;
  z-index: 1051; }

.select2-results {
  display: block; }

.select2-results__options {
  list-style: none;
  margin: 0;
  padding: 0; }

.select2-results__option {
  padding: 6px;
  user-select: none;
  -webkit-user-select: none; }
  .select2-results__option[aria-selected] {
    cursor: pointer; }

.select2-container--open .select2-dropdown {
  left: 0; }

.select2-container--open .select2-dropdown--above {
  border-bottom: none;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0; }

.select2-container--open .select2-dropdown--below {
  border-top: none;
  border-top-left-radius: 0;
  border-top-right-radius: 0; }

.select2-search--dropdown {
  display: block;
  padding: 4px; }
  .select2-search--dropdown .select2-search__field {
    padding: 4px;
    width: 100%;
    box-sizing: border-box; }
    .select2-search--dropdown .select2-search__field::-webkit-search-cancel-button {
      -webkit-appearance: none; }
  .select2-search--dropdown.select2-search--hide {
    display: none; }

.select2-close-mask {
  border: 0;
  margin: 0;
  padding: 0;
  display: block;
  position: fixed;
  left: 0;
  top: 0;
  min-height: 100%;
  min-width: 100%;
  height: auto;
  width: auto;
  opacity: 0;
  z-index: 99;
  background-color: #fff;
  filter: alpha(opacity=0); }

.select2-container--default .select2-selection--single {
  background-color: #fff;
  border: 1px solid #aaa;
  border-radius: 4px; }
  .select2-container--default .select2-selection--single .select2-selection__rendered {
    color: #444;
    line-height: 28px; }
  .select2-container--default .select2-selection--single .select2-selection__clear {
    cursor: pointer;
    float: right;
    font-weight: bold; }
  .select2-container--default .select2-selection--single .select2-selection__placeholder {
    color: #999; }
  .select2-container--default .select2-selection--single .select2-selection__arrow {
    height: 26px;
    position: absolute;
    top: 1px;
    right: 1px;
    width: 20px; }
    .select2-container--default .select2-selection--single .select2-selection__arrow b {
      border-color: #888 transparent transparent transparent;
      border-style: solid;
      border-width: 5px 4px 0 4px;
      height: 0;
      left: 50%;
      margin-left: -4px;
      margin-top: -2px;
      position: absolute;
      top: 50%;
      width: 0; }
.select2-container--default[dir="rtl"] .select2-selection--single .select2-selection__clear {
  float: left; }
.select2-container--default[dir="rtl"] .select2-selection--single .select2-selection__arrow {
  left: 1px;
  right: auto; }
.select2-container--default.select2-container--disabled .select2-selection--single {
  background-color: #eee;
  cursor: default; }
  .select2-container--default.select2-container--disabled .select2-selection--single .select2-selection__clear {
    display: none; }
.select2-container--default.select2-container--open .select2-selection--single .select2-selection__arrow b {
  border-color: transparent transparent #888 transparent;
  border-width: 0 4px 5px 4px; }
.select2-container--default .select2-selection--multiple {
  background-color: white;
  border: 1px solid #aaa;
  border-radius: 4px;
  cursor: text; }
  .select2-container--default .select2-selection--multiple .select2-selection__rendered {
    box-sizing: border-box;
    list-style: none;
    margin: 0;
    padding: 0 5px;
    width: 100%; }
  .select2-container--default .select2-selection--multiple .select2-selection__placeholder {
    color: #999;
    margin-top: 5px;
    float: left; }
  .select2-container--default .select2-selection--multiple .select2-selection__clear {
    cursor: pointer;
    float: right;
    font-weight: bold;
    margin-top: 5px;
    margin-right: 10px; }
  .select2-container--default .select2-selection--multiple .select2-selection__choice {
    background-color: #e4e4e4;
    border: 1px solid #aaa;
    border-radius: 4px;
    cursor: default;
    float: left;
    margin-right: 5px;
    margin-top: 5px;
    padding: 0 5px; }
  .select2-container--default .select2-selection--multiple .select2-selection__choice__remove {
    color: #999;
    cursor: pointer;
    display: inline-block;
    font-weight: bold;
    margin-right: 2px; }
    .select2-container--default .select2-selection--multiple .select2-selection__choice__remove:hover {
      color: #333; }
.select2-container--default[dir="rtl"] .select2-selection--multiple .select2-selection__choice, .select2-container--default[dir="rtl"] .select2-selection--multiple .select2-selection__placeholder {
  float: right; }
.select2-container--default[dir="rtl"] .select2-selection--multiple .select2-selection__choice {
  margin-left: 5px;
  margin-right: auto; }
.select2-container--default[dir="rtl"] .select2-selection--multiple .select2-selection__choice__remove {
  margin-left: 2px;
  margin-right: auto; }
.select2-container--default.select2-container--focus .select2-selection--multiple {
  border: solid black 1px;
  outline: 0; }
.select2-container--default.select2-container--disabled .select2-selection--multiple {
  background-color: #eee;
  cursor: default; }
.select2-container--default.select2-container--disabled .select2-selection__choice__remove {
  display: none; }
.select2-container--default.select2-container--open.select2-container--above .select2-selection--single, .select2-container--default.select2-container--open.select2-container--above .select2-selection--multiple {
  border-top-left-radius: 0;
  border-top-right-radius: 0; }
.select2-container--default.select2-container--open.select2-container--below .select2-selection--single, .select2-container--default.select2-container--open.select2-container--below .select2-selection--multiple {
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0; }
.select2-container--default .select2-search--dropdown .select2-search__field {
  border: 1px solid #aaa; }
.select2-container--default .select2-search--inline .select2-search__field {
  background: transparent;
  border: none;
  outline: 0; }
.select2-container--default .select2-results > .select2-results__options {
  max-height: 200px;
  overflow-y: auto; }
.select2-container--default .select2-results__option[role=group] {
  padding: 0; }
.select2-container--default .select2-results__option[aria-disabled=true] {
  color: #999; }
.select2-container--default .select2-results__option[aria-selected=true] {
  background-color: #ddd; }
.select2-container--default .select2-results__option .select2-results__option {
  padding-left: 1em; }
  .select2-container--default .select2-results__option .select2-results__option .select2-results__group {
    padding-left: 0; }
  .select2-container--default .select2-results__option .select2-results__option .select2-results__option {
    margin-left: -1em;
    padding-left: 2em; }
    .select2-container--default .select2-results__option .select2-results__option .select2-results__option .select2-results__option {
      margin-left: -2em;
      padding-left: 3em; }
      .select2-container--default .select2-results__option .select2-results__option .select2-results__option .select2-results__option .select2-results__option {
        margin-left: -3em;
        padding-left: 4em; }
        .select2-container--default .select2-results__option .select2-results__option .select2-results__option .select2-results__option .select2-results__option .select2-results__option {
          margin-left: -4em;
          padding-left: 5em; }
          .select2-container--default .select2-results__option .select2-results__option .select2-results__option .select2-results__option .select2-results__option .select2-results__option .select2-results__option {
            margin-left: -5em;
            padding-left: 6em; }
.select2-container--default .select2-results__option--highlighted[aria-selected] {
  background-color: #5897fb;
  color: white; }
.select2-container--default .select2-results__group {
  cursor: default;
  display: block;
  padding: 6px; }

.select2-container--classic .select2-selection--single {
  background-color: #f6f6f6;
  border: 1px solid #aaa;
  border-radius: 4px;
  outline: 0;
  background-image: -webkit-linear-gradient(top, #ffffff 50%, #eeeeee 100%);
  background-image: -o-linear-gradient(top, #ffffff 50%, #eeeeee 100%);
  background-image: linear-gradient(to bottom, #ffffff 50%, #eeeeee 100%);
  background-repeat: repeat-x;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#eeeeee', GradientType=0); }
  .select2-container--classic .select2-selection--single:focus {
    border: 1px solid #5897fb; }
  .select2-container--classic .select2-selection--single .select2-selection__rendered {
    color: #444;
    line-height: 28px; }
  .select2-container--classic .select2-selection--single .select2-selection__clear {
    cursor: pointer;
    float: right;
    font-weight: bold;
    margin-right: 10px; }
  .select2-container--classic .select2-selection--single .select2-selection__placeholder {
    color: #999; }
  .select2-container--classic .select2-selection--single .select2-selection__arrow {
    background-color: #ddd;
    border: none;
    border-left: 1px solid #aaa;
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
    height: 26px;
    position: absolute;
    top: 1px;
    right: 1px;
    width: 20px;
    background-image: -webkit-linear-gradient(top, #eeeeee 50%, #cccccc 100%);
    background-image: -o-linear-gradient(top, #eeeeee 50%, #cccccc 100%);
    background-image: linear-gradient(to bottom, #eeeeee 50%, #cccccc 100%);
    background-repeat: repeat-x;
    filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#eeeeee', endColorstr='#cccccc', GradientType=0); }
    .select2-container--classic .select2-selection--single .select2-selection__arrow b {
      border-color: #888 transparent transparent transparent;
      border-style: solid;
      border-width: 5px 4px 0 4px;
      height: 0;
      left: 50%;
      margin-left: -4px;
      margin-top: -2px;
      position: absolute;
      top: 50%;
      width: 0; }
.select2-container--classic[dir="rtl"] .select2-selection--single .select2-selection__clear {
  float: left; }
.select2-container--classic[dir="rtl"] .select2-selection--single .select2-selection__arrow {
  border: none;
  border-right: 1px solid #aaa;
  border-radius: 0;
  border-top-left-radius: 4px;
  border-bottom-left-radius: 4px;
  left: 1px;
  right: auto; }
.select2-container--classic.select2-container--open .select2-selection--single {
  border: 1px solid #5897fb; }
  .select2-container--classic.select2-container--open .select2-selection--single .select2-selection__arrow {
    background: transparent;
    border: none; }
    .select2-container--classic.select2-container--open .select2-selection--single .select2-selection__arrow b {
      border-color: transparent transparent #888 transparent;
      border-width: 0 4px 5px 4px; }
.select2-container--classic.select2-container--open.select2-container--above .select2-selection--single {
  border-top: none;
  border-top-left-radius: 0;
  border-top-right-radius: 0;
  background-image: -webkit-linear-gradient(top, #ffffff 0%, #eeeeee 50%);
  background-image: -o-linear-gradient(top, #ffffff 0%, #eeeeee 50%);
  background-image: linear-gradient(to bottom, #ffffff 0%, #eeeeee 50%);
  background-repeat: repeat-x;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#eeeeee', GradientType=0); }
.select2-container--classic.select2-container--open.select2-container--below .select2-selection--single {
  border-bottom: none;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0;
  background-image: -webkit-linear-gradient(top, #eeeeee 50%, #ffffff 100%);
  background-image: -o-linear-gradient(top, #eeeeee 50%, #ffffff 100%);
  background-image: linear-gradient(to bottom, #eeeeee 50%, #ffffff 100%);
  background-repeat: repeat-x;
  filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#eeeeee', endColorstr='#ffffff', GradientType=0); }
.select2-container--classic .select2-selection--multiple {
  background-color: white;
  border: 1px solid #aaa;
  border-radius: 4px;
  cursor: text;
  outline: 0; }
  .select2-container--classic .select2-selection--multiple:focus {
    border: 1px solid #5897fb; }
  .select2-container--classic .select2-selection--multiple .select2-selection__rendered {
    list-style: none;
    margin: 0;
    padding: 0 5px; }
  .select2-container--classic .select2-selection--multiple .select2-selection__clear {
    display: none; }
  .select2-container--classic .select2-selection--multiple .select2-selection__choice {
    background-color: #e4e4e4;
    border: 1px solid #aaa;
    border-radius: 4px;
    cursor: default;
    float: left;
    margin-right: 5px;
    margin-top: 5px;
    padding: 0 5px; }
  .select2-container--classic .select2-selection--multiple .select2-selection__choice__remove {
    color: #888;
    cursor: pointer;
    display: inline-block;
    font-weight: bold;
    margin-right: 2px; }
    .select2-container--classic .select2-selection--multiple .select2-selection__choice__remove:hover {
      color: #555; }
.select2-container--classic[dir="rtl"] .select2-selection--multiple .select2-selection__choice {
  float: right; }
.select2-container--classic[dir="rtl"] .select2-selection--multiple .select2-selection__choice {
  margin-left: 5px;
  margin-right: auto; }
.select2-container--classic[dir="rtl"] .select2-selection--multiple .select2-selection__choice__remove {
  margin-left: 2px;
  margin-right: auto; }
.select2-container--classic.select2-container--open .select2-selection--multiple {
  border: 1px solid #5897fb; }
.select2-container--classic.select2-container--open.select2-container--above .select2-selection--multiple {
  border-top: none;
  border-top-left-radius: 0;
  border-top-right-radius: 0; }
.select2-container--classic.select2-container--open.select2-container--below .select2-selection--multiple {
  border-bottom: none;
  border-bottom-left-radius: 0;
  border-bottom-right-radius: 0; }
.select2-container--classic .select2-search--dropdown .select2-search__field {
  border: 1px solid #aaa;
  outline: 0; }
.select2-container--classic .select2-search--inline .select2-search__field {
  outline: 0; }
.select2-container--classic .select2-dropdown {
  background-color: white;
  border: 1px solid transparent; }
.select2-container--classic .select2-dropdown--above {
  border-bottom: none; }
.select2-container--classic .select2-dropdown--below {
  border-top: none; }
.select2-container--classic .select2-results > .select2-results__options {
  max-height: 200px;
  overflow-y: auto; }
.select2-container--classic .select2-results__option[role=group] {
  padding: 0; }
.select2-container--classic .select2-results__option[aria-disabled=true] {
  color: grey; }
.select2-container--classic .select2-results__option--highlighted[aria-selected] {
  background-color: #3875d7;
  color: white; }
.select2-container--classic .select2-results__group {
  cursor: default;
  display: block;
  padding: 6px; }
.select2-container--classic.select2-container--open .select2-dropdown {
  border-color: #5897fb; }
<link href="https://ajax.googleapis./ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.rawgit./select2/select2/master/dist/js/select2.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/jqueryui/1.11.2/jquery-ui.min.js"></script>

<select id="selectb">
  <option value="1">Select2</option>
  <option value="2">Chosen</option>
  <option value="4">selectize.js</option>
  <option value="6">typeahead.js</option>
</select>

When you apply select2 on a 'select' tag it hides the actual tag and create it's own 'input' tag with 'options' down under it. So when you are hovering on a select2 , you are actually hovering on the 'input' tag created by the select2 not on the actual 'select' tag you wrote. To make title visible you will have to add title to the 'input' tag. Find the 'input' created by the select2 and add title on that.

Use the following

<select id="daySelect">
<optgroup label="select your day">

Try doing it using select2 options and methods:

JS:

$("#daySelect").select2().on('select2:select', function(evt) {
  $(this).attr("title",$(this).val());
});

Demo:http://jsfiddle/lotusgodkk/fyhsz9ra/1432/

Its simple ,on change add the current value of select as its title attribute,

Run snippet, change the option and hover on the select you can see the change

$('#daySelect').change(function(){
	 $(this).next().attr('title',$(this).val());
	});
	$('#daySelect').select2().change();
<link href="https://cdnjs.cloudflare./ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet"/>

<script src="https://ajax.googleapis./ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/select2/4.0.3/js/select2.full.min.js"></script>
 <select id="daySelect">
	 <option val="0">Sunday</option>
	 <option val="1">Monday</option>
	 <option val="2">Tuesday</option>
	 <option val="3">Wednesday</option>
	 <option val="4">Thursday</option>
	 <option val="5">Friday</option>
	 <option val="6">Saturday</option>
</select>

let newOption = new Option("text", null, true, true); //.setAttribute("title", "test|title ll|gg");
            newOption.setAttribute("title", "test|title ll|gg");

Then when you have multiple you can put it in a a each loop.

Add 'disabled' attribute to the first tag to bee the header like this-

<select id="planets" name="planets" > 
        <option disabled>Habitable</option>
        <option value="1">Earth</option>    
        <option value="2">Mars</option> 

        <option disabled>Inhabitable</option>
        <option value="3">Saturn</option>    
        <option value="4">Pluto</option>                  
</select>

The info in the code snippet above is only for demonstration purposes only

本文标签: javascriptAdding title to select2 drop downStack Overflow