自动完成“设置”,jquery

发布于 2024-08-06 16:59:14 字数 1063 浏览 2 评论 0原文

我正在尝试使用我正在构建的简单应用程序自动完成工作。到目前为止,这是我的代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故事和酒 2024-08-13 16:59:14

几天前,我在 jquery 自动完成方面遇到了同样的问题,以至于我考虑使用脚本自动完成,但事实证明它比我想象的要容易得多 - 对我的所有拉扯感到有点惊讶经历过:)

事实证明,这就是你所需要的,说真的......如果你遇到问题,请回复我,我会尽力与你一起解决。

准备好您的 .php 文件。就我而言,我将其称为“list.php”
我的表称为列表,有字段 id 和 name。我只检索名称。这些名称将填充可选选项。

请注意,用户在输入框中键入的内容将作为“q”传递。这就是您在下面代码的第 2 行中看到的 $_GET['q']。如果需要,您可以覆盖/重命名它,但最好不要打扰。请注意,它也与输入字段本身的名称无关。

另请注意,jquery 自动完成需要“\n”将结果分隔为独立可选择的选项。如果您不连接“\n”,它将全部输出为单个可选选项。

//start with database connection of course
$rs = mysql_query("SELECT * FROM lists WHERE name LIKE '%" . $_GET['q'] . "%'");


while($row = mysql_fetch_assoc($rs)) {
    echo $row['name'] . "\n";
}

准备好您的输入字段。就我而言,我将其称为 name="myinputfield" 但这并不重要,因为名称并不重要,重要的是 id="searchterm"

<input name="myinputfield" type="text" id="searchterm" size="30" maxlength="100" />

然后在您的 .js 文件中,包括以下内容:

$("#searchterm").autocomplete("list.php");

我做了一些额外的事情样式等,因为我对默认样式不满意,但这就是实现功能所需的全部。最重要的一点是在脚本中使用 $_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.

//start with database connection of course
$rs = mysql_query("SELECT * FROM lists WHERE name LIKE '%" . $_GET['q'] . "%'");


while($row = mysql_fetch_assoc($rs)) {
    echo $row['name'] . "\n";
}

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

<input name="myinputfield" type="text" id="searchterm" size="30" maxlength="100" />

Then in your .js file, include the following:

$("#searchterm").autocomplete("list.php");

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.

行至春深 2024-08-13 16:59:14

您应该尝试的第一件事是:

gMeds = JSON.parse(result).meds;

您还应该将代码移至 success 方法中。

$(document).ready(function(){   
    setUpAutoComplete();
});

function setUpAutoComplete() {
    $.ajax({
        url: "ajax-getAllMeds.php",
        async: false,
        type: "GET",
        dataType: "text",
        success: function(result){
            var json = JSON.parse(result);
                if (!json || !json.meds) {
                    alert('invalid');
                }
                $("#med").autocomplete(json.meds);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
            return false;
        }
    });
}

The first thing you should try is this:

gMeds = JSON.parse(result).meds;

You should also move the code into the success method.

$(document).ready(function(){   
    setUpAutoComplete();
});

function setUpAutoComplete() {
    $.ajax({
        url: "ajax-getAllMeds.php",
        async: false,
        type: "GET",
        dataType: "text",
        success: function(result){
            var json = JSON.parse(result);
                if (!json || !json.meds) {
                    alert('invalid');
                }
                $("#med").autocomplete(json.meds);
        },
        error: function (XMLHttpRequest, textStatus, errorThrown) {
            alert(textStatus);
            return false;
        }
    });
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文