2016-06-29 46 views
0

我想嘗試創建一個自定義的jQuery UI自動完成,但我在第一步本身失敗。當我點擊填充的值時,沒有響應。我無法選擇自動填充的值。任何人都可以讓我知道爲什麼?無法選擇jQuery自動完成的值?

下面是我的代碼:

HTML:

<!DOCTYPE html> 
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script> 
    <title>test</title> 
<body> 
    <div>Select:</div> 
    <input id="project" style="width:380px;"> 
</body> 

的Javascript:

(function($){ 

    var $project = $('#project'); 

    var projects = [ 
    { 
     value: "test1", 
     label: "test1", 
     desc: "test1", 
     icon: "jquery_32x32.png" 
    }, 
    { 
     value: "test2", 
     label: "test2", 
     desc: "test2", 
     icon: "jqueryui_32x32.png" 
    } 
    ]; 

    $project.autocomplete({ 
    minLength: 0, 
    source: projects, 
    focus: function(event, ui) { 
     $project.val(ui.item.value); 
     return false; 
    } 
    }).data("ui-autocomplete")._renderItem = function(ul, item) { 
    var inner_html = '<table><tr><td>' + item.value + '</td></tr></table>'; 
return $("<li></li>") 
           .data("item.autocomplete", item) 
           .append(inner_html) 
           .appendTo(ul); 
    }; 


})(jQuery); 
+0

[?工作正常(https://jsfiddle.net/16e97Lja/) –

回答

1

您只需要更換:

<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script> 
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.min.js"></script> 

有:

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script> 
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script> 

檢查以下小提琴:

(function($){ 
 

 
    var $project = $('#project'); 
 

 
    var projects = [ 
 
    { 
 
     value: "test1", 
 
     label: "test1", 
 
     desc: "test1", 
 
     icon: "jquery_32x32.png" 
 
    }, 
 
    { 
 
     value: "test2", 
 
     label: "test2", 
 
     desc: "test2", 
 
     icon: "jqueryui_32x32.png" 
 
    } 
 
    ]; 
 

 
    $project.autocomplete({ 
 
    minLength: 0, 
 
    source: projects, 
 
    focus: function(event, ui) { 
 
     $project.val(ui.item.value); 
 
     return false; 
 
    } 
 
    }).data("ui-autocomplete")._renderItem = function(ul, item) { 
 
    var inner_html = '<table><tr><td>' + item.value + '</td></tr></table>'; 
 
return $("<li></li>") 
 
           .data("item.autocomplete", item) 
 
           .append(inner_html) 
 
           .appendTo(ul); 
 
    }; 
 

 

 
})(jQuery);
<head> 
 
\t \t <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> 
 
\t \t <script src="http://code.jquery.com/jquery-2.1.4.min.js"></script> 
 
\t \t <script src="http://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script> 
 
    \t \t <title>test</title> 
 
\t </head> 
 
\t <body> 
 
    \t \t <div>Select:</div> 
 
    \t \t <input id="project" style="width:380px;"> 
 
\t </body>

+0

砰上..!謝謝:) – sTg