javascript Jquery UI 自动完成输入 - 如何在更改时检查输入是否为空?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6851645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Jquery UI autocomplete input - How to check if input is empty on change?
提问by Danny
I have a Jquery UI auto complete field on my page, and I need to do something when input becomes empty (i.e The user typed something in, then deleted all of it).
我的页面上有一个 Jquery UI 自动完成字段,当输入变空时我需要做一些事情(即用户输入了一些东西,然后删除了所有内容)。
This event must fire regardless of whether what they typed in initially produced autocomplete results.
无论他们最初输入的内容是否产生自动完成结果,都必须触发此事件。
The point is to show a button if there are no autocomplete results for typed entry, and remove it if there are results OR the input is empty.
关键是如果输入的条目没有自动完成结果,则显示一个按钮,如果有结果或输入为空,则将其删除。
UPDATE I have tried this...
更新我试过这个......
$('#employeeAutocomplete').change(function() {
alert("eventFired");
if($( "#employeeAutocomplete" ).val() == ''){
$('#createEmployeeBtn').hide();
}
});
And alert is not displayed....
并且不显示警报....
Thanks, Danny
谢谢,丹尼
回答by niklasdstrom
http://forum.jquery.com/topic/autocomplete-and-change-event
http://forum.jquery.com/topic/autocomplete-and-change-event
About the same problem:
关于同样的问题:
I've found the change: does work, but only when the input loses focus.
The solution I implemented, to get the effect I desired was to implement my own check as part of the source: function. I set minLength: to 0, and if the length was less than 2 I loaded my default display data.
我发现了变化:确实有效,但仅当输入失去焦点时。
我实现的解决方案是实现我自己的检查作为 source: 函数的一部分来获得我想要的效果。我将 minLength: 设置为 0,如果长度小于 2,我将加载我的默认显示数据。
回答by Milox
Take a look at this plugin
看看这个插件
http://www.zurb.com/playground/jquery-text-change-custom-event
http://www.zurb.com/playground/jquery-text-change-custom-event
it's a closer handler for real textchange event
它是真正的 textchange 事件的更接近处理程序
回答by shadow69
I know this post is really old but i was trying the same think and the answers here didn't pleased me.
我知道这篇文章真的很旧,但我也在尝试同样的想法,这里的答案并不让我满意。
So for myself, i did the test of empty value on blur event of jquery.
所以对于我自己,我对jquery的blur事件做了空值的测试。
$('.input').focus(function() {
$(this).autocomplete({
// autocomplete code here
})
}).blur(function() {
if($(this).val() == "") {
}
});
$('.input').focus(function() {
$(this).autocomplete({
// autocomplete code here
})
}).blur(function() {
if($(this).val() == "") {
}
});
回答by Charles Robertson
OK people. The following definitely works and is an amazingly simple & elegant solution, requiring a couple of lines of code.
好的人。以下绝对有效,是一个非常简单和优雅的解决方案,需要几行代码。
100% guaranteed to work.
100%保证工作。
@Milox has already mentioned it:
@Milox 已经提到过:
http://zurb.com/playground/jquery-text-change-custom-event
http://zurb.com/playground/jquery-text-change-custom-event
This jQuery plugin is tiny. Once you download/copy it & link to the 'js' file, use the following:
这个 jQuery 插件很小。下载/复制它并链接到“js”文件后,请使用以下命令:
jQuery('#input').bind('notext',function () {
console.log('text field is empty');
});
There are other configuarations, but this was all I needed. Boom. Sorted.
还有其他配置,但这就是我所需要的。繁荣。排序。
回答by VAD
Super old question, but I think I found a solution that worked great for me and may help others. So what made it work for me without dancing around autocomplete
and what does not seem to have the flaws that change
and keypress
had, was keyup
.
超级老问题,但我想我找到了一个对我有用并且可以帮助其他人的解决方案。那么是什么让这对我的工作没有跳舞周围autocomplete
,似乎什么不有缺陷,change
并且keypress
有,是keyup
。
Simply adding keyup
event handler next to autocomplete
block makes the event fire regardless loosing a focus or changing the field value, even with clearing the value. This makes the feature work in all the cases I could think of. Here's the code:
简单地keyup
在autocomplete
块旁边添加事件处理程序会使事件触发,无论失去焦点或更改字段值,即使清除值也是如此。这使得该功能在我能想到的所有情况下都能正常工作。这是代码:
$('#employeeAutocomplete').keyup(function() {
if($( "#employeeAutocomplete" ).val() === '') {
$('#createEmployeeBtn').hide();
}
});
回答by mkubasch
I have solved this issue with autocomplete close event ! Close fired when result list is empty. Im my case this correspond with an empty input field.
我已经通过自动完成关闭事件解决了这个问题!当结果列表为空时关闭触发。我的情况这对应于一个空的输入字段。
回答by Grigor
Hope this helps you :) .change() - jQuery API
希望这对你有帮助:) .change() - jQuery API
回答by Grigor
回答by Maxx
$('#employeeAutocomplete').change(function() {
alert("eventFired");
if(this.value == ''){
$('#createEmployeeBtn').hide();
}
});
回答by Milox
jquery ui's autocomplete has his own change event, have you tried it?
jquery ui 的自动完成有他自己的更改事件,你试过了吗?
$( ".selector" ).autocomplete({
change: function(event, ui) { ... }
});