jquery 自动完成中的文本字段锁定 - 仅允许在自动完成选项内提交
嘿,我正在我的代码中寻求帮助,该代码允许仅在指定的数据数组内自动完成。因此,只允许输入自动完成数组内的数据。例如,如果用户输入 iran01,则用户将不允许输入 01,因为 iran01 无效。或者例如,如果用户在 iran 之后输入 NNN,则它会是 iranNNN,这也是无效的。那么只有伊朗应该出现在有效条目的文本框中。任何人都可以告诉如何做到这一点。
任何人都可以告诉如何获得这种行为。
$(document).ready(function () {
data = ["iran", "iraq", "Libya", "india"];
$("#autocomplete").autocomplete(data)
});
$('input#autocomplete').bind('keypress keyup', function(){
if ($.inArray($(this).val(), data) == -1){
$(this).val('');
$("#result").html("No match found!" + $(this).val());
}
});
$('input#autocomplete').result(function (event, data, formatted) {
$("#result").html(!data ? "No match found!" : "Selected: " + formatted);
}).keyup(function () {
$(this).search();
});
}
});
Hei, am looking for help in my code that allows an autocomplete, only within a specified data array. So only data within the auto complete array is allowed to be entered. for example if user has typed iran01 then user wont allow to type 01 because iran01 is not valid . or for example if user type NNN after iran then it wud be iranNNN which is also not valid . Then only iran shud be in textbox which is valid entry . can anyone tell how to do this.
can any one tell how to get this behavior .
$(document).ready(function () {
data = ["iran", "iraq", "Libya", "india"];
$("#autocomplete").autocomplete(data)
});
$('input#autocomplete').bind('keypress keyup', function(){
if ($.inArray($(this).val(), data) == -1){
$(this).val('');
$("#result").html("No match found!" + $(this).val());
}
});
$('input#autocomplete').result(function (event, data, formatted) {
$("#result").html(!data ? "No match found!" : "Selected: " + formatted);
}).keyup(function () {
$(this).search();
});
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在输入框的更改事件上放置一个侦听器,然后确保已选择某个元素。看看我对此问题的回答:
jquery 自动完成字段需要选定的值?
http://jsfiddle.net/vZeHr/4/
You can put put a listener on the change event of your input box and then make sure an element has been selected. Check out my answer to this question:
jquery autocomplete field that REQUIRES a selected value?
http://jsfiddle.net/vZeHr/4/