jQuery UI自动完成JSON数组与JS-Array

时间:2015-10-13 18:36:06

标签: javascript arrays json user-interface autocomplete

Hoi伙计们,我没有向js证实。我的问题是,如果我在它工作的代码中定义一个自动完成的数组,如果我使用json-array(也来自外部源)它的剂量。我做错了什么?

 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

   });
 });

1 个答案:

答案 0 :(得分: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">
&#13;
&#13;
&#13;