gpt4 book ai didi

jquery - 更新 Twitter 预输入的每个按键上的 JSON

转载 作者:行者123 更新时间:2023-12-01 04:47:29 24 4
gpt4 key购买 nike

我有一个带有输入字段的 html 页面。每次在此字段中输入内容时,都会使用 jQuery 调用 php 脚本。

此 PHP 脚本返回一个 JSON,其中包含基于输入字段的特定查询的结果。

这工作正常,我正在控制台中记录输出。

<html>
<head>
<script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
<script>
var r = jQuery(function($) {
$('#name').keyup(function() {
var input = $('#name').val();

var response = $.getJSON('get_names.php', {input: input});

response.done(function (names) {
console.log(names);
});
});
});
</script>

</head>

<body>
<div><input id="name"></div>
</body>
</html>

现在的问题是,我需要将其与 twitter bootstrap - first example in typeahead.js 合并,而不是登录到 console ,这样每次使用的 json(下面代码中的 states)都会由 php 脚本根据字段的内容进行计算。

$(document).ready(function() {

var substringMatcher = function(strs) {
return function findMatches(q, cb) {
var matches, substringRegex;

// an array that will be populated with substring matches
matches = [];

// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');

// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array
$.each(strs, function(i, str) {
if (substrRegex.test(str)) {
// the typeahead jQuery plugin expects suggestions to a
// JavaScript object, refer to typeahead docs for more info
matches.push({ value: str });
}
});

cb(matches);
};
};

var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California',
'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii',
'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana',
'Maine', 'Maryland', 'Massachusetts', 'Michigan', 'Minnesota',
'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire',
'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota',
'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island',
'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont',
'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming'
];

$('#the-basics .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
},
{
name: 'states',
displayKey: 'value',
source: substringMatcher(states)
});
});

我一直试图用这个来捕捉值(value),但没有任何改变:

jQuery(function($) {
$('#name').keyup(function() {
var input = $('#name').val();

names = $.getJSON('get_names.php', {input: input});

});
});

那么:如何通过使用到目前为止编写的输入调用 php 脚本,使每次发生 keyup 事件时更新 JSON?

最佳答案

编辑、更新

尝试

    var substringMatcher = function () {
return function findmatches(q, cb) {
var matches, substrRegex;

// an array that will be populated with substring matches
matches = [];

// regex used to determine if a string contains the substring `q`
substrRegex = new RegExp(q, 'i');

// iterate through the pool of strings and for any string that
// contains the substring `q`, add it to the `matches` array

// do ajax stuff
// update remote `source` here
$.post("/echo/json/"
// do stuff with `q` : `input`
, {json:JSON.stringify([states, {input:q}])})
.then(function(json) {
var names = json[1];
console.log(names);
$.each(json[0], function (i, str) {
if (substrRegex.test(str)) {
// the typeahead jQuery plugin expects suggestions to a
// JavaScript object, refer to typeahead docs for more info
matches.push({
value: str
});
}
});

cb(matches);
} // `error` handler
, function(jqxhr, textStatus, errorThrown) {
console.log(textStatus, errorThrown)
}); // end of `then`
};

};

var states = ['Alabama', 'Alaska', 'Arizona', 'Arkansas'
, 'California','Colorado', 'Connecticut', 'Delaware'
, 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois'
, 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana'
, 'Maine', 'Maryland', 'Massachusetts', 'Michigan'
, 'Minnesota', 'Mississippi', 'Missouri', 'Montana'
, 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey'
, 'New Mexico', 'New York', 'North Carolina', 'North Dakota'
, 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvania', 'Rhode Island'
, 'South Carolina', 'South Dakota', 'Tennessee', 'Texas'
, 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia'
, 'Wisconsin', 'Wyoming'];

$('#the-basics .typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
name: 'categories',
displayKey: 'value',
source: substringMatcher()
});

jsfiddle http://jsfiddle.net/guest271314/96d5mzr8/16/

关于jquery - 更新 Twitter 预输入的每个按键上的 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27347121/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com