自动完成“设置”,jquery
我正在尝试使用我正在构建的简单应用程序自动完成工作。到目前为止,这是我的代码:
gMeds = new Array(); $(document).ready(function(){ var autoComp = setUpAutoComplete(); if(autoComp) { $("#med").autocomplete(gMeds); } else { alert('Autocomplete unavailable'); } }); function setUpAutoComplete() { $.ajax({ url: "ajax-getAllMeds.php", async: false, type: "GET", dataType: "text", success: function(result){ gMeds = JSON.parse(result); return true; }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(textStatus); return false; } }); }
“ajax-getAllMeds.php”的源生成有效的 JSON(由 http:// /www.jsonlint.com/)。
生成的 JSON 是
{ "meds": [ { "name": "ace" }, { "name": "danger" } ] }
我想要完成的是将我的 JSON 转换为 JavaScript 数组,然后使用该数组作为我的单词池“自动完成”的基础。我还差得远吗?我遇到了各种问题。
I'm trying to get auto-complete working with a simple application that I'm building. Here is my code so far:
gMeds = new Array(); $(document).ready(function(){ var autoComp = setUpAutoComplete(); if(autoComp) { $("#med").autocomplete(gMeds); } else { alert('Autocomplete unavailable'); } }); function setUpAutoComplete() { $.ajax({ url: "ajax-getAllMeds.php", async: false, type: "GET", dataType: "text", success: function(result){ gMeds = JSON.parse(result); return true; }, error: function (XMLHttpRequest, textStatus, errorThrown) { alert(textStatus); return false; } }); }
The source of "ajax-getAllMeds.php" produces valid JSON (as verified by http://www.jsonlint.com/).
The JSON produced is
{ "meds": [ { "name": "ace" }, { "name": "danger" } ] }
What I'm trying to accomplish is turning my JSON into a javascript array and then using that array as the basis for my pool of words to "autocomplete from". Am I way off? I'm running into various problems.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
几天前,我在 jquery 自动完成方面遇到了同样的问题,以至于我考虑使用脚本自动完成,但事实证明它比我想象的要容易得多 - 对我的所有拉扯感到有点惊讶经历过:)
事实证明,这就是你所需要的,说真的......如果你遇到问题,请回复我,我会尽力与你一起解决。
准备好您的 .php 文件。就我而言,我将其称为“list.php”
我的表称为列表,有字段 id 和 name。我只检索名称。这些名称将填充可选选项。
请注意,用户在输入框中键入的内容将作为“q”传递。这就是您在下面代码的第 2 行中看到的 $_GET['q']。如果需要,您可以覆盖/重命名它,但最好不要打扰。请注意,它也与输入字段本身的名称无关。
另请注意,jquery 自动完成需要“\n”将结果分隔为独立可选择的选项。如果您不连接“\n”,它将全部输出为单个可选选项。
准备好您的输入字段。就我而言,我将其称为 name="myinputfield" 但这并不重要,因为名称并不重要,重要的是 id="searchterm"
然后在您的 .js 文件中,包括以下内容:
我做了一些额外的事情样式等,因为我对默认样式不满意,但这就是实现功能所需的全部。最重要的一点是在脚本中使用 $_GET['q'] 并在 js 中定位 #searchterm id。
让我知道这是否可以解决您的问题。
I was having the same problem with jquery autocomplete a couple of days ago to the point that I considered using the scriptaculous auto-complete, but it turned out to be a lot easier than I thought actually - kind of surprised at all the hair pulling I went through :)
As it turns out, this is all you need, seriously.. if you run into problems get back to me and I'll try to work it out with you.
Get your .php file ready. In my case, I called it "list.php"
My table is called lists and has fields id and name. I'm retrieving the name only. These names are what's going to populate the selectable options.
Note that what the user types into the input box is passed as 'q'. This is the $_GET['q'] you see in line 2 of the code below. You can override/rename it if you want, but better to not bother. Note that it also has nothing to do with the name of the input field itself.
Note also that jquery autocomplete needs the "\n" to separate the results into independently selectable options. If you don't concatenate "\n", it will output all as a single selectable option.
Get your input field ready. In my case, I called it name="myinputfield" but that's not important because the name doesn't matter, it's the id="searchterm" that matters
Then in your .js file, include the following:
I did some extra stuff with the styling etc. because I wasn't happy with the default styling, but that's all you need to get the functionalirty going. The most important points are to use $_GET['q'] in your script and target the #searchterm id in your js.
Let me know if this solves your problem.
您应该尝试的第一件事是:
您还应该将代码移至 success 方法中。
The first thing you should try is this:
You should also move the code into the success method.