向 jQuery-UI 1.8.1 添加自动填充功能
这就是我目前所拥有的,不幸的是我似乎无法弄清楚如何让 autoFill
与 jQuery-UI 一起使用...它曾经与
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" type="text/javascript"></script>
<script src="http://jquery-ui.googlecode.com/svn/tags/latest/external/jquery.bgiframe-2.1.1.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/i18n/jquery-ui-i18n.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
var thesource = "RegionsAutoComplete.axd?PID=3"
$(function () {
function log(message) {
$("<div/>").text(message).prependTo("#log");
$("#log").attr("scrollTop", 0);
}
$.expr[':'].textEquals = function (a, i, m) {
return $(a).text().match("^" + m[3] + "$");
};
$("#regions").autocomplete({
source: thesource,
change: function (event, ui) {
//if the value of the textbox does not match a suggestion, clear its value
if ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0) {
$(this).val('');
}
else {
//THIS IS CURRENTLY NOT "LOGGING" THE "UI" DATA
//IF THE USER DOES NOT EXPLICITLY SELECT
//THE SUGGESTED TEXT
//PLEASE HELP!!!
log(ui.item ? ("Selected: " + ui.item.value + " aka " + ui.item.id) : "Nothing selected, input was " + this.value);
}
}
}).live('keydown', function (e) {
var keyCode = e.keyCode || e.which;
//if TAB or RETURN is pressed and the text in the textbox does not match a suggestion, set the value of the textbox to the text of the first suggestion
if ((keyCode == 9 || keyCode == 13) && ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0)) {
$(this).val($(".ui-autocomplete li:visible:first").text());
}
});
});
</script>
后端的直接 Autocomplete.js My JSON 一起使用看起来像这样
[
{ "label": "Canada", "value": "Canada", "id": "1" },
{ "label": "United States", "value": "United States", "id": "2" },
]
我已经使用了答案此处 使 MustMatch 正常工作,但不幸的是,如果我“制表符”离开输入框,或者如果我完全键入单词而不是实际选择建议的文本,我会得到“未选择任何内容”响应,而不是值和 ID。
有谁知道当您实际上没有选择该字段时如何从自动完成中提取 id ?
基本上,我正在寻找的内容与此处找到的 Month (local):
示例类似: http://jquery.bassistance.de/autocomplete/demo/
但显然使用 jQuery-UI 而不是 jquery.autocomplete.js< /强>
here's what I currently have, unfortunately I cannot seem to figure out how to get autoFill
to work with jQuery-UI... It used to work with the straight up Autocomplete.js
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js" type="text/javascript"></script>
<script src="http://jquery-ui.googlecode.com/svn/tags/latest/external/jquery.bgiframe-2.1.1.js" type="text/javascript"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/i18n/jquery-ui-i18n.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
var thesource = "RegionsAutoComplete.axd?PID=3"
$(function () {
function log(message) {
$("<div/>").text(message).prependTo("#log");
$("#log").attr("scrollTop", 0);
}
$.expr[':'].textEquals = function (a, i, m) {
return $(a).text().match("^" + m[3] + "$");
};
$("#regions").autocomplete({
source: thesource,
change: function (event, ui) {
//if the value of the textbox does not match a suggestion, clear its value
if ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0) {
$(this).val('');
}
else {
//THIS IS CURRENTLY NOT "LOGGING" THE "UI" DATA
//IF THE USER DOES NOT EXPLICITLY SELECT
//THE SUGGESTED TEXT
//PLEASE HELP!!!
log(ui.item ? ("Selected: " + ui.item.value + " aka " + ui.item.id) : "Nothing selected, input was " + this.value);
}
}
}).live('keydown', function (e) {
var keyCode = e.keyCode || e.which;
//if TAB or RETURN is pressed and the text in the textbox does not match a suggestion, set the value of the textbox to the text of the first suggestion
if ((keyCode == 9 || keyCode == 13) && ($(".ui-autocomplete li:textEquals('" + $(this).val() + "')").size() == 0)) {
$(this).val($(".ui-autocomplete li:visible:first").text());
}
});
});
</script>
My JSON in the back end looks like this
[
{ "label": "Canada", "value": "Canada", "id": "1" },
{ "label": "United States", "value": "United States", "id": "2" },
]
I've used the answer here to get the mustMatch working, but unfortunately if I "tab" away from the input box or if I type the word completely rather than actually selecting the suggested text, I get the "Nothing selected" response instead of an Value and ID.
Does anyone know how to extract the id out of the autocomplete when you don't actually select the field?
Basically, What I'm looking for is something similar to the Month (local):
example found HERE: http://jquery.bassistance.de/autocomplete/demo/
But obviously using the jQuery-UI instead of the jquery.autocomplete.js
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
演示: http://jsbin.com/akile3更新 2: 支持 TAB! ;)
演示: http://jsbin.com/akile3/8
尝试这样的事情:
不确定是否是您想要的,无论如何,如果匹配某些内容,它会自动填充整个单词!
DEMO: http://jsbin.com/akile3UPDATED 2: with TAB support! ;)
DEMO: http://jsbin.com/akile3/8
try something like this:
not sure if is what you want, anyway it autofill entire word if match something!
如果您只是想自动填充,这就是我正在使用的代码。
If you simply want to autofill this is the code I am using.
自动填充功能在 JQuery UI 版本的自动完成功能中已被弃用,并且不再支持 jquery.autocomplete 插件。
这里是 github 的链接解决方案。 如果您按 Tab 键跳出字段,并且将“selectFirst: true”设置为自动完成调用的选项,这将自动选择列表中的第一项。
AutoFill has been deprecated in the JQuery UI version of autocomplete, and the jquery.autocomplete plugin is no longer supported.
Here is a link to a github solutiuon. This will automatically select the first item in the list if you tab out of the field, and have "selectFirst: true" set as an option on your autocomplete call.