[英]Autocomplete jQueryUI double call function on select and press enter
I'd like some little help. 我需要一点帮助。 Here's my JsFiddle : https://jsfiddle.net/Mirawen/q59nqh2z/
这是我的JsFiddle: https ://jsfiddle.net/Mirawen/q59nqh2z/
function keyPress(keyEvent)
{
if (keyEvent.which == 13)
MainFunction();
}
function MainFunction()
{
alert("Hello World");
}
var arrSource = ["foo", "foooo", "foooooo", "bar", "barr", "barr"];
$(document).ready(function() {
$("#txtSearch").autocomplete({
source: arrSource,
minLength: 1,
select: function (event, ui)
{
MainFunction();
}
});
});
When I press enter, I want to call MainFunction()
, and also call this function when I select an item from autocomplete The point is, when I select an item and press enter, MainFunction()
is called twice. 当我按下Enter键时,我想调用
MainFunction()
,并且在我从自动完成功能中选择一个项目时也要调用此函数。要点是,当我选择一个项目并按Enter键时, MainFunction()
被调用了两次。
I don't know how to solve this. 我不知道该怎么解决。
Thanks for your answers ! 感谢您的回答!
I think that you are calling the same function twice on same event,so it is calling main function twice. 我认为您在同一事件上两次调用相同的函数,因此它两次调用主函数。 Even if you press enter it is a keypress/keyup/keydown event.
即使您按Enter键,它也是按键/键上/键下事件。 So, when document is ready, it is getting called already once and gets called on your enter keypress event.
因此,当文档准备就绪时,它已经被调用一次,并在您的enter keypress事件上被调用。
function addEnterKeyHandler() { $( "#txtSearch" ).keypress(function( event ) { if ( event.which == 13 ) { MainFunction(); } }); } function removeEnterKeyHandler(){ $('#txtSearch').unbind("keypress"); } function MainFunction() { alert("Hello World"); } var arrSource = ["foo", "foooo", "foooooo", "bar", "barr", "barr"]; $(document).ready(function() { addEnterKeyHandler(); $("#txtSearch").autocomplete( { source: arrSource, minLength: 1, selecting: function(event, ui){ canPressEnter = false; }, select: function (event, ui) { removeEnterKeyHandler(); MainFunction(); addEnterKeyHandler(); } }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.11.4/jquery-ui.min.js" integrity="sha256-xNjb53/rY+WmG+4L6tTl9m6PpqknWZvRt0rO1SRnJzw=" crossorigin="anonymous"></script> <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.css"> <div> <input type="text" id="txtSearch" onkeypress="keyPress(event);" /> <button type="button" title="Search" onclick="MainFunction();">Search </button> </div>
So it seems that I solve my issue ! 所以看来我解决了我的问题!
In my MainFunction(), I've an AJAX call (ASP.NET) with async false. 在我的MainFunction()中,我使用异步false进行了AJAX调用(ASP.NET)。 I tried to change this call async to true.
我试图将此调用异步更改为true。 Now, only the event
autocompleteselect
is triggered ! 现在,只有事件
autocompleteselect
被触发!
But, I don't have any clue why keypress
isn't triggered anymore. 但是,我不知道为什么不再触发
keypress
。 I'll keep looking, and edit this answer if I find why async false also trigger keypress
after autocompleteselect
, in case if someone else has the same issue. 我会继续寻找并编辑此答案,如果我发现为什么async false也会在
autocompleteselect
之后触发keypress
,以防其他人遇到相同的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.