jquery 的事件顺序
我有以下情况:
您可以在其中输入关键字的文本框。 keyup 事件触发一个 AJAX 脚本,该脚本在数据库中查找类似的关键字。找到后,DIV 将与找到的关键字一起可见 (show()
)。
当单击 div 中的关键字之一时,所选关键字将被写入文本框中,并且 div 将被隐藏 (hide()
)。
但是,当 DIV 可见并且我单击表单中的其他位置或按 Tab 键移出文本框(焦点事件)时,我希望隐藏 DIV。
这可以通过 focusout 事件来完成。但是,当我使用 click 事件注册 DIV 中关键字的点击并使用 focusout 事件注册 focusout 事件时,会出现以下问题:当单击 div 中的关键字时,将触发 focusout 事件,因此我的 DIV 将被触发隐藏,但关键字不会复制到文本框中。
我目前有以下代码:
/*click on found "link with class f_link" in DIV*/
$(".f_link").live('click', function(){
$newval=$(this).attr("id");
$("#textbox_id").val($newval);
$("#searchresults").hide();
})
/*when losing focus textbox hide DIV */
$("#textbox_id").focusout(function(){
$("#searchresults").hide();
})
解释:
#textbox_id : id of textbox
#searchresults: id of DIV with found results
.f_link : class of "link" in div searchresults like <span class="f_link" id="result1>result 1</span>
I have the following situation:
Textbox where you can type a keyword. The keyup event triggers an AJAX script which looks in the database for similar keywords. When found a DIV will be visible (show()
) with the found keywords.
When clicking on one of the keywords in the div the selected keyword will be written in the textbox and the div will be hidden (hide()
).
But when the DIV is visible and I click somewhere else in the form or I tab out of the textbox (the focusout event) I would like the DIV to be hidden.
This could be done with the focusout event. But when I use the click event for registering the click on a keyword in the DIV and using the focusout event for registering the focusout event the following problem occurs: When clicking the keyword in the div the focusout event will be triggered so my DIV will be hidden but the keyword will not be copied to the textbox.
I currently have the following code:
/*click on found "link with class f_link" in DIV*/
$(".f_link").live('click', function(){
$newval=$(this).attr("id");
$("#textbox_id").val($newval);
$("#searchresults").hide();
})
/*when losing focus textbox hide DIV */
$("#textbox_id").focusout(function(){
$("#searchresults").hide();
})
explaination:
#textbox_id : id of textbox
#searchresults: id of DIV with found results
.f_link : class of "link" in div searchresults like <span class="f_link" id="result1>result 1</span>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试:
Try:
模糊不起作用(focusout也是jquery中的一个事件..)
我用这个解决方案管理它:
Blur didn't work (focusout is an event in jquery as well..)
I managed it with this solution:
您可以使用模糊功能代替焦点...
You can use blur function istead of focusout...