2015-10-13 78 views
-1

海关人员,我不确认js。我的问题是如果我在代码中定义一个自动完成的数组,它的工作原理是,如果我使用json数组(也来自外部源)它是剂量。我究竟做错了什么 ?jQuery UI自动完成JSON数组与JS阵列

jsonData='{"kantone":["VD","FR","GE"]} 
var alternate=["TG","ZG","ZH"]; 
window.availableKanton = JSON.parse(jsonData); 
$(function() { 
$("#startkanton").autocomplete({ 
    source: window.availableKanton.kantone // dont work if i take the alternate it does 

    }); 
}); 

回答

0

我把你的代码粘贴到下面的代码片段中,它正在工作。

我唯一要做的就是关闭字符串(将'放到第一行)。

var jsonData = '{"kantone":["VD","FR","GE"]}'; 
 
var alternate = ["TG", "ZG", "ZH"]; 
 

 
window.availableKanton = JSON.parse(jsonData); 
 

 
$(function() { 
 
    $("#startkanton").autocomplete({ 
 
    source: window.availableKanton.kantone // working 
 
    }); 
 
});
<link href="https://code.jquery.com/ui/1.11.4/themes/black-tie/jquery-ui.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> 
 
<script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js"></script> 
 
<input id="startkanton">