admin管理员组文章数量:1289875
I have a form working with JQuery Autoplete and it works fairly well. Now I need another element to force a user to select a valid choice in the autoplete input. The can type whatever they want and everything is filtered by autoplete. But they have to select something from the served list. If they don't the inputfield must get blanked. Tried out a few things with change and select to no avail. This is the code of my autoplete. I saw some examples operation with data instead of source. This seems to make a big difference
$(function () {
$("#sp_name").autoplete({
minLength: 2,
delay: 300,
source: function (request, response) {
$.ajax({
url: "./Search/",
dataType: "json",
data: {
term: request.term,
zoekterm: $("#al").html()
},
success: function (data) {
response($.map(data, function (item) {
return {
label: item.naam,
value: item.naam
}
}));
}
});
}
})
});
I have a form working with JQuery Autoplete and it works fairly well. Now I need another element to force a user to select a valid choice in the autoplete input. The can type whatever they want and everything is filtered by autoplete. But they have to select something from the served list. If they don't the inputfield must get blanked. Tried out a few things with change and select to no avail. This is the code of my autoplete. I saw some examples operation with data instead of source. This seems to make a big difference
$(function () {
$("#sp_name").autoplete({
minLength: 2,
delay: 300,
source: function (request, response) {
$.ajax({
url: "./Search/",
dataType: "json",
data: {
term: request.term,
zoekterm: $("#al").html()
},
success: function (data) {
response($.map(data, function (item) {
return {
label: item.naam,
value: item.naam
}
}));
}
});
}
})
});
Share
Improve this question
asked Sep 10, 2013 at 13:31
PattuxPattux
1251 gold badge1 silver badge5 bronze badges
5
-
you can bind
blur
event to your autoplete input, in your case$("#sp_name")
, then determine whether it has perfered value or not – jasonslyvia Commented Sep 10, 2013 at 13:35 - Should I do that with another JSON call? Or is there some other smart way? – Pattux Commented Sep 10, 2013 at 13:46
-
nope, let's assume your
$("#sp_name")
input expect user to select a name from the autoplete suggestion list, then you can bindblur
event to your input like `$("#sp_name").blur(function(){ if (!this.value){/* deal with the case user doesn't choose anything*/}}); – jasonslyvia Commented Sep 10, 2013 at 13:48 - What if they input something that's not in the suggestion list? – Pattux Commented Sep 10, 2013 at 13:52
-
1
i myself usually add a
hidden input
to store theid
or something of that choice, by determine thehidden input
, i can tell from whether user choose from the autoplete list or not – jasonslyvia Commented Sep 10, 2013 at 13:57
1 Answer
Reset to default 8Try this:
I am using a local list here, but you can achieve this via json data source too. Just add a change event. The only problem this can have is that if user does not click on the suggestion, it will turn blank (even if user is entering the same text as suggestion). It will be mandatory for user to click on the suggestion.
var list = ["c", "c++", "c#","Basic","Mongo"];
$('#auto').autoplete({
source: list,
select: function (event, ui) {
$(this).val(ui.item ? ui.item : " ");},
change: function (event, ui) {
if (!ui.item) {
this.value = '';}
//else { Return your label here }
}
});
JsFidle for it: http://jsfiddle/sarcastic/7KdZP/112/
In your case, Change function would be something like this:
change: function (event, ui)
{
if (!ui.label) { this.value = ''; }
}
本文标签: javascriptJQuery AutocompleteForce choiceStack Overflow
版权声明:本文标题:javascript - JQuery Autocomplete - Force choice - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741448330a2379336.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论