<script>
$(function() {
var myArray = '[{"OriginId":2609,"OriginName":"14th Mile Stone"},{"OriginId":2007,"OriginName":"Aachara"},{"OriginId":2220,"OriginName":"Aarni"},{"OriginId":2216,"OriginName":"Aasind"},{"OriginId":637,"OriginName":"Aathankarai"},{"OriginId":1292,"OriginName":"Aatthur"},{"OriginId":1144,"OriginName":"Aavanam"},{"OriginId":2909,"OriginName":"Abad (Airport)"},{"OriginId":379,"OriginName":"Abiramam"},{"OriginId":4556,"OriginName":"ABLOLI"},{"OriginId":4554,"OriginName":"ABLOLI KALE HOUSE"},{"OriginId":2346,"OriginName":"Abohar"},{"OriginId":2500,"OriginName":"Abu Road"},{"OriginId":4395,"OriginName":"ACHALPUR"},{"OriginId":1594,"OriginName":"Achanta"},{"OriginId":2769,"OriginName":"Adda Road"}]';
var jsonString = JSON.parse(myArray);
$('#busPoint').autocomplete({
source: function (request, response) {
$.getJSON(jsonString, function (data) {
console.log(data)
response($.map(data.list, function (value, key) {
return {
label: value,
value: key
};
}));
});
},
minLength: 2,
delay: 100
});
});
</script>
<!doctype html>
<html lang="en">
<head>
<title>jQuery UI Autocomplete</title>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
</head>
<body>
<div class="ui-widget">
<hr />
<input type="text" id="busPoint">
</div>
</body>
</html>
我的HTML代碼
<div class="ui-widget">
<hr />
<input type="text" id="busPoint">
</div>
我的Javascript代碼
<script>
$(function() {
var myArray = '[{"OriginId":2609,"OriginName":"14th Mile Stone"},{"OriginId":2007,"OriginName":"Aachara"},{"OriginId":2220,"OriginName":"Aarni"},{"OriginId":2216,"OriginName":"Aasind"},{"OriginId":637,"OriginName":"Aathankarai"},{"OriginId":1292,"OriginName":"Aatthur"},{"OriginId":1144,"OriginName":"Aavanam"},{"OriginId":2909,"OriginName":"Abad (Airport)"},{"OriginId":379,"OriginName":"Abiramam"},{"OriginId":4556,"OriginName":"ABLOLI"},{"OriginId":4554,"OriginName":"ABLOLI KALE HOUSE"},{"OriginId":2346,"OriginName":"Abohar"},{"OriginId":2500,"OriginName":"Abu Road"},{"OriginId":4395,"OriginName":"ACHALPUR"},{"OriginId":1594,"OriginName":"Achanta"},{"OriginId":2769,"OriginName":"Adda Road"}]';
var jsonString = JSON.parse(myArray);
$('#busPoint').autocomplete({
source: function (request, response) {
$.getJSON(jsonString, function (data) {
response($.map(data, function (value, key) {
return {
label: value,
value: key
};
}));
});
},
minLength: 2,
delay: 100
});
});
</script>
我試過這段代碼,但它不工作。 我需要提取選擇的originid和名稱。 大部分示例都包含到外部json或webservice拉的連接,但我的數據位於同一頁面上。 如果他們是任何其他我可以使用此外,請讓我知道
你可以創建jsfiddle來調試 – rahulsm
添加請檢查 –