2016-11-26 34 views
0

我使用的代碼示例發現here並希望添加一個函數,當用戶將鼠標懸停在組合框中的選項上時,將創建一個帶有消息的工具提示。工具提示必須出現在懸停的每個選項上。jQuery組合框 - 事件(鼠標輸入/懸停)列表中的選項

在這個階段,我只想讓事件發生,其中的信息可以是我以後會定義的任何東西。有人可以幫忙嗎?

這裏是我的代碼:

 $(function() { 
 
     $.widget("custom.combobox", { 
 
      _create: function() { 
 
      this.wrapper = $("<span>") 
 
       .addClass("custom-combobox") 
 
       .insertAfter(this.element); 
 
     
 
      this.element.hide(); 
 
      this._createAutocomplete(); 
 
      this._createShowAllButton(); 
 
      this._hoverOption(); 
 
      }, 
 
    \t 
 
    \t 
 
    /* 
 
    * 
 
    * Hover Event 
 
    * 
 
    */ 
 
      _hoverOption: function() {}, 
 
    \t 
 
    
 
      _createAutocomplete: function() { 
 
      var selected = this.element.children(":selected"), 
 
       value = selected.val() ? selected.text() : ""; 
 
     
 
      this.input = $("<input>") 
 
       .appendTo(this.wrapper) 
 
       .val(value) 
 
       .attr("title", "") 
 
       .addClass("custom-combobox-input ui-widget ui-widget-content ui-state-default ui-corner-left") 
 
       .autocomplete({ 
 
       delay: 0, 
 
       minLength: 0, 
 
       source: $.proxy(this, "_source") 
 
       }) 
 
       .tooltip({ 
 
       classes: { 
 
        "ui-tooltip": "ui-state-highlight" 
 
       } 
 
       }); 
 
     
 
      this._on(this.input, { 
 
       autocompleteselect: function(event, ui) { 
 
       ui.item.option.selected = true; 
 
       this._trigger("select", event, { 
 
        item: ui.item.option 
 
       }); 
 
       }, 
 
     
 
       autocompletechange: "_removeIfInvalid" 
 
      }); 
 
      }, 
 
     
 
      _createShowAllButton: function() { 
 
      var input = this.input, 
 
       wasOpen = false; 
 
     
 
      $("<a>") 
 
       .attr("tabIndex", -1) 
 
       .attr("title", "Show All Items") 
 
       .tooltip() 
 
       .appendTo(this.wrapper) 
 
       .button({ 
 
       icons: { 
 
        primary: "ui-icon-triangle-1-s" 
 
       }, 
 
       text: false 
 
       }) 
 
       .removeClass("ui-corner-all") 
 
       .addClass("custom-combobox-toggle ui-corner-right") 
 
       .on("mousedown", function() { 
 
       wasOpen = input.autocomplete("widget").is(":visible"); 
 
       }) 
 
       .on("click", function() { 
 
       input.trigger("focus"); 
 
     
 
       // Close if already visible 
 
       if (wasOpen) { 
 
        return; 
 
       } 
 
     
 
       // Pass empty string as value to search for, displaying all results 
 
       input.autocomplete("search", ""); 
 
       }); 
 
      }, 
 
     
 
      _source: function(request, response) { 
 
      var matcher = new RegExp($.ui.autocomplete.escapeRegex(request.term), "i"); 
 
    \t \t var result = []; 
 
      result = this.element.children("option").map(function() { 
 
       var text = $(this).text(); 
 
       if (this.value && (!request.term || matcher.test(text))) 
 
       return { 
 
        label: text, 
 
        value: text, 
 
        option: this 
 
       }; 
 
      }); 
 
    \t \t if(result.length == 1 && request.term.length > this.options.previousValue.length){ 
 
    \t \t \t \t result[0].option.selected = true; 
 
    \t \t \t \t this._trigger("select", null, { 
 
    \t \t \t \t \t item: result[0].option 
 
    \t \t \t \t }); 
 
    \t \t \t \t this.input.val(result[0].label); 
 
    \t \t \t \t this.options.previousValue = result[0].label; 
 
    \t \t }else{ 
 
    \t \t \t this.options.previousValue = request.term; \t \t 
 
    \t \t } 
 
    \t \t response(result); 
 
    \t \t 
 
      }, 
 
     
 
      _removeIfInvalid: function(event, ui) { 
 
     
 
      // Selected an item, nothing to do 
 
      if (ui.item) { 
 
       return; 
 
      } 
 
     
 
      // Search for a match (case-insensitive) 
 
      var value = this.input.val(), 
 
       valueLowerCase = value.toLowerCase(), 
 
       valid = false; 
 
      this.element.children("option").each(function() { 
 
       if ($(this).text().toLowerCase() === valueLowerCase) { 
 
       this.selected = valid = true; 
 
       return false; 
 
       } 
 
      }); 
 
     
 
      // Found a match, nothing to do 
 
      if (valid) { 
 
       return; 
 
      } 
 
     
 
      }, 
 
     
 
      _destroy: function() { 
 
      this.wrapper.remove(); 
 
      this.element.show(); 
 
      } 
 
     }); 
 
     
 
     $("#combobox").combobox(); 
 
    
 
     });
li.ui-menu-item { 
 
\t padding-left: 15px; \t 
 
} 
 

 
.custom-combobox { 
 
\t position: relative; 
 
\t display: inline-block; 
 
\t margin-right: 40px; 
 
    } 
 
    
 
.custom-combobox-toggle { 
 
    position: absolute; 
 
    top: 0; 
 
    bottom: 0; 
 
    margin-left: -1px; 
 
    padding: 0; 
 
    } 
 

 
.custom-combobox-input { 
 
    margin: 0; 
 
    padding: 5px 10px; 
 
    }
<script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
 

 
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> 
 
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylesheet"/> 
 

 

 

 
    <select id="combobox"> 
 
    <option value="">Select one...</option> 
 
    <option value="good">one</option> 
 
    <option value="bad">two</option> 
 
    <option value="ugly">three</option> 
 

 
    </select>

在解決這個要記住的是,jQuery的程序隱藏原來的名單是很重要的。它創建一個具有ul li結構的新列表。

因此,創建選項列表的事件不適用於懸停,因爲用戶無法與原始列表進行交互,因爲jQuery代表用戶執行此操作。

當用戶選擇一個選項時,jQuery更新原始選項列表。

因此,爲了使mouseover/hover/mouseenter事件在這裏工作,它必須綁定到由jQuery創建的列表。

回答

0

嘗試mouseover()jQuery方法。你

https://api.jquery.com/mouseover/

$(document).ready(function(){ 
    //For each option in the combobox select 
    $('select#combobox option').each(function(){ 
     // attach an mouseover event 
     $(this).on('mouseover', function(){ 
      //your tooltip logic 

     }); 
    }); 
}); 

小提琴:http://jsfiddle.net/4BTyH/308/

+0

恐怕它不會起火。 –

+0

@JacquesJoubert當你將鼠標懸停在選項上時(它不會點擊選項),它應該會觸發。 – ScanQR

+0

@JacquesJoubert嘗試更新的答案。 – ScanQR

0

解決了!

看到我的初始後上面的代碼

查找:

_hoverOption: function() {}, 

替換:

_hoverOption: function() { 

     var input = this.input; 

     input.autocomplete("widget").each(function() { 

      $(this).on('mouseenter', "li", function(){ 

       //tooltip logic 

       }); 

     }); 

     }, 
0

有可能填補工具提示,同時數據的每個項目填充組合框:

$.each(prdtFiltered, function() { 
         producto.append($("<option />").val(this.cod_producto).text(this.producto_nombre).attr("title",this.descripcion)); 
        }); 

在這個例子中,var prdtFIltered是json。