2013-03-01 81 views
2

我有一个自动完成的基本文本框,列出了彩虹的颜色,但我想改变它,这样当你选择一种颜色时,它会以这种颜色出现(即红色字为红色),但我不知道该怎么做。格式化数组中的文本?

继承人的代码:

<!doctype html> 

<html lang="en"> 
<head> 
    <meta charset="utf-8" /> 
    <title>jQuery UI Autocomplete - Default functionality</title> 
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.1/themes/base/jquery-ui.css" /> 
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script> 
    <script src="http://code.jquery.com/ui/1.10.1/jquery-ui.js"></script> 
<link rel="stylesheet" href="/resources/demos/style.css" /> 
    <script> 
     $(function() { 
      var colours = [ 
      "red", 
      "orange", 
      "yellow", 
      "green", 
      "blue", 
      "indigo", 
      "violet", 
      ] 

      $("#rainbow1").autocomplete({    
       source: colours 
      }); 
     }); 

    </script> 
</head> 
<body> 

<div class="ui-widget"> 
    <label for="rainbow1" style="color:red">Rainbow1: </label> 
    <input type="text" id="rainbow1" /> </br> 
</div> 


</body> 
</html> 
+0

欢迎来到StackOverflow。你能缩小你的代码到你的问题,并将其添加到你的问题? – thordarson 2013-03-01 12:39:28

+0

谢谢。那么,代码发布编译和运行,因为它应该。我问什么要添加到它来改变自动完成中的文本的颜色。 – Mufasatheking 2013-03-01 12:44:03

回答

1

看到这个:http://jsfiddle.net/gkxe4/1/

$(function() { 
    var colours = [ 
     "red", 
     "orange", 
     "yellow", 
     "green", 
     "blue", 
     "indigo", 
     "violet", ]; 

    $("#rainbow1").autocomplete({ 
     source: colours, 
     open: function (event, ui) { 
      $(".ui-autocomplete li > a").each(function() { 
       $(this).css('color', $(this).text()); 
      }); 
     }, 
     select: function (event, ui) { 
      alert(ui.item.value); 
      $("#rainbow1").css("color", ui.item.value); 
     }, 
     search: function() { 
      $(this).css('color', ''); 
     } 
    }); 
}); 
+0

这个作品非常完美,完全如我所愿,非常感谢。 – Mufasatheking 2013-03-01 14:26:19

+0

欢迎标记为答案,如果解决.. – Anujith 2013-03-01 14:27:06

0
you can do this on blur event.... 

$("#rainbow1").blur(function() { 
$(this).css('background',$(this).val()); 
}); 
0

基本例如:

$("#rainbow1").autocomplete({ 
    source: colours, 
    select: function(event, ui) { 
     $(this).css('color', ui.item.value); 
    }, 
    search: function() { 
     $(this).css('color', '') 
    } 
}); 

你需要自动完成的selectsearch选择玩。

http://jsfiddle.net/dfsq/eSzyV/

0

你自动完成申报应包含select财产。用这种方法你可以在用户选择一个项目时改变颜色。

$("#rainbow1").autocomplete({ 
    source: colours, 
    select: function(event, ui) { 
     $("#rainbow1").css("color", ui.item.label); 
    } 
}); 

查看演示here

Docs here