Jquery ui 自动完成让我发疯

发布于 2024-11-16 07:13:36 字数 631 浏览 0 评论 0原文

我有一个

我在我的 .ready 函数中

$("#txtCustome2r").autocomplete({
    source:  "itemcomplete.asp", 
    minLength: 2,
    select: function( event, ui ) {
        log( ui.item ?
                     "Selected: " + ui.item.value + " aka " + ui.item.id :
                     "Nothing selected, input was " + this.value );
    }
}); 

自动完成返回有效的 json

[ { "id': "4",  "label": "Kathi  ",   "value": "Kathi  "}, { "id': "6",  "label": "Kathleen  ",   "value": "Kathleen  "}]

,并且下拉列表中没有显示任何内容。非常感谢任何帮助!

谢谢!

I have a <input id="txtCustome2r" />

I have in my .ready function

$("#txtCustome2r").autocomplete({
    source:  "itemcomplete.asp", 
    minLength: 2,
    select: function( event, ui ) {
        log( ui.item ?
                     "Selected: " + ui.item.value + " aka " + ui.item.id :
                     "Nothing selected, input was " + this.value );
    }
}); 

the auto complete return valid json

[ { "id': "4",  "label": "Kathi  ",   "value": "Kathi  "}, { "id': "6",  "label": "Kathleen  ",   "value": "Kathleen  "}]

and nothing shows up in the drop down. Any help is greatly appreciated!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

孤云独去闲 2024-11-23 07:13:36

单引号不是有效的 JSON。您需要用双引号将键名和字符串值引起来:

[ { "id": 4,  "label": "Kathi", "value": "Kathi 3" }, ... ]

如果您想检查 JSON 响应的有效性,可以使用 JSONLint

Single quotes are not valid JSON. You'll need to surround your key names and string values with double quotes:

[ { "id": 4,  "label": "Kathi", "value": "Kathi 3" }, ... ]

If you want to check your JSON response for validity, you can use JSONLint.

甜中书 2024-11-23 07:13:36

如果按照 @Mark Bell 的解决方案中所述验证格式后仍然遇到问题,请尝试将 dataType: 'json' 传递给自动完成函数调用。

If you are still having problems after verifying your format as mentioned in @Mark Bell's solution, try passing in dataType: 'json' to the autocomplete function call.

这可能有点棘手。我喜欢将源代码变成一个函数,这样我就可以有更多的控制权。注意 toString 的覆盖:

var search = function (request, response) {
    jQuery.get(
        jQuery('#SearchUrl').val(),
        { searchString: request.term },
        function (data) {
            response(jQuery.map(data.searchResults, function (item) {
                return {
                    label: item.Id,
                    value: {
                        toString: function () { return item.Id + ' - ' + item.Name; },
                        Name: item.Name
                    }
                }
            }));
        }
    );
};
// set up the autocomplete
jQuery('#MyTextBox').autocomplete({
    source: search,
    minLength: 3,
    focus: function (event, ui) {
        jQuery('#name').text(ui.item.value.Name);
    }
});

This can be a little tricky. I like making the source a function so I can have more control. Notice the override on toString:

var search = function (request, response) {
    jQuery.get(
        jQuery('#SearchUrl').val(),
        { searchString: request.term },
        function (data) {
            response(jQuery.map(data.searchResults, function (item) {
                return {
                    label: item.Id,
                    value: {
                        toString: function () { return item.Id + ' - ' + item.Name; },
                        Name: item.Name
                    }
                }
            }));
        }
    );
};
// set up the autocomplete
jQuery('#MyTextBox').autocomplete({
    source: search,
    minLength: 3,
    focus: function (event, ui) {
        jQuery('#name').text(ui.item.value.Name);
    }
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文