javascript 清除预先输入字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27781213/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 07:59:16  来源:igfitidea点击:

Clear typeahead field

javascriptjquerytypeahead.js

提问by Rafael Roque

how could I clear a typeahead field in a focusoutevent?

如何清除focusout事件中的预先输入字段?

The following jQuery code doesn′t seem to work in a typeahead field:

下面的 jQuery 代码似乎在 typeahead 字段中不起作用:

$( "#field" ).focusout(function() {
    $(this).val("");
});

采纳答案by Diego Rodriguez

try with this, example here in fiddle

试试这个,在小提琴中的例子

$('.typeahead').typeahead().bind('typeahead:closed', function () {
    $(this).val("");
});

回答by Brent Matzelle

The accepted answer is now out of date. See the latest Typeahead documentationto see that trapping the "close" event and setting the value of the input are different. Here's an equivalent, updated answer:

已接受的答案现已过时。查看最新的 Typeahead 文档,可以看到捕获“close”事件和设置输入值是不同的。这是一个等效的更新答案:

$('.typeahead').typeahead().bind('typeahead:close', function() {
    $('.typeahead').typeahead('val', '');
});

回答by iNandi

Thanks for all the answers, but the closeevent did not work for me maybe my setting or something has deprecated. so I have written this code and it worked well.

感谢所有的答案,但关闭事件对我不起作用,也许我的设置或某些东西已被弃用。所以我写了这段代码并且运行良好。

$('#typeahead-id').typeahead().bind('typeahead:selected', function() {
    $('#typeahead-id').typeahead('val', '')
});