2011-07-25 40 views
1

我想改變源xml文件中報告的數據後提出的聲音的顏色。我的代碼可以解釋我的問題更好:jquery ui自動完成樣式選擇顏色

$("#search_input").autocomplete({ 
    source: function(req, add){ 
    //pass request to server 
    $.get(stringa, req, function(xml) { 
      var dato = $("contratto", xml).map(function() { 
        return { 
          label: $("id", this).text()+ ", "+ $("nominativo", this).text(), 
          value: $("id", this).text(), 
          cls: $("class", this).text() 
        }; 
      }).get(); 
      //pass array to callback 
      add(dato); 
     }); 
    }, 
    minLength: 0, 
    select: function(event, ui) { 
     $("#search_input").val(ui.item.label); 
      $("#hidden_contratto").val(ui.item.value); 
      //$(".ui-menu-item >a").css("color", "red"); 
    }, 
    open: function(event, ui) { 
     switch (ui.item.cls) 
     { 
     //here must change color to propose if eoncontered cls=red 
      case "red" : 
       $(".ui-autocomplete li.ui-menu-item a").css({"color": "red", "text-decoration": "line-through"}); 
      break; 
     } 
    } 
}); 

這裏源XML:

<contratto> 
    <id>M12125</id> 
    <nominativo>my name</nominativo> 
    <class>red</class> 
</contratto> 

在Firebug我得到這個錯誤:ui.item未定義

回答

2

幾個attemps我發現後,我解!

我在許多coverted類,並以這種方式糾正我的代碼:

open: function(event, ui) { 
      var voce=""; 
      var v=""; 
      $("li > a").each(function(){ 
       v=$(this).text(); 
       voce=v.split(","); 
       if (voce[2]==1) 
        $(this).css({"color": "green", "text-decoration": "underline"}); 
       if (voce[2]==2) 
        $(this).css({"color": "red", "text-decoration": "line-through"}); 
       $(this).text(voce[0]+", "+voce[1]); 
      }); 
     } 

我希望這能幫助別人! h。

ciao h。

0
 ,open: function(e, ui) { 
      var data = $(this).data('autocomplete');    
      data.menu.element.find('li').each(function() { 
       var $this = $(this); 
       var matched = data.term; 
       var rest = $this.text().replace(matched, ''); 
       var template = $('<span/>', { 
        'class': 'ui-found', 
        'text': matched 
       }).after($('<span/>', { 
        'text': rest  
       })); 
       $this.html(template); 
      }); 
     } 
+0

我是通過這個網站找到的。我希望這段代碼能幫助你 – hasunjjang