如何防止在附加事件的情况下在下拉列表上触发 jquery 事件
我的网页上有一个下拉列表,其中有一个附加到更改事件的 jquery 事件。在更改时,我使用 ajax 在弹出窗口中加载部分视图。
当用户使用键盘浏览项目列表,然后单击页面另一部分上的链接时,我会立即看到弹出窗口。这显然是发生的,因为当用户单击页面上其他位置的链接时,首先在下拉列表中触发更改事件。
有什么巧妙的解决方案来解决这个问题吗?一个简单的解决方案是在 keyup 上触发事件,但我不希望发生此功能。
这是更改事件的代码片段
$('#lookup').change(function()
{
$.get(..//load template
I have a drop down list on my webpage with a jquery event attached to the change event. On change I use ajax to load a partial view in a popup window.
When the user uses the keyboard to navigate through the list of items but then clicks a link on another part of the page I momentarily see my popup window. This is obviously happening as when the user clicks a link elsewhere on the page, the change event is triggered on the drop down first.
Are there any neat solutions to solve this problem? A simple solution would be to fire the event on keyup but I don't want this functionality to happen.
Here's the change event snippet of code
$('#lookup').change(function()
{
$.get(..//load template
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果是 ajax 调用,则您的 ajax 调用有可能被缓存。确保您正在禁用缓存。类似“缓存:假”
If its an ajax call, there is chance that your ajax call getting cached. Make sure you are disabling cache. something like "cache:false"
该事件无法停止,因为在任何情况下都会触发更改事件。
一种可能是在事件触发和实际调用 $.get 方法之间设置一定的超时,以便考虑您的情况,但这当然不是一个优雅的解决方案。
否则,只有当某个其他元素获得焦点时(例如,如果下拉列表后面有一个文本框),您才可以调用返回函数。
我不会太在意它,因为您的弹出窗口似乎必须在下拉菜单更改上显示。如果不是很重要,您可能会在下拉菜单的右侧有一个帮助按钮,单击按钮会弹出窗口。
The event could not be stopped because the change event is fired in any case.
One possibility could be to set a certain timeout between the event fired and the actual call of the $.get method in order to take your case into account, but certainly it isn't an elegant solution.
Otherwise you may call the return function only if a certain other element gets the focus (for example if there is a textbox, after your dropdown list).
I would not care too much about it as it seems your popup HAS to be shown on Dropdown change. If it is not critical, you may have a help button on dropdown's right that popsup your window on button's click.
我对你的问题有点困惑。但假设“更改”不是真正的更改,我认为您可以为“lastSelected”保留一个变量,并且当发生更改时,对其进行检查。如果相同则忽略。如果不同,请触发 Ajax 请求并更新“lastSelected”的值。
I'm a little confused by your question. But assuming that the "change" isn't a true change, I think you could keep a variable for "lastSelected" and, when a change occurs, check it against that. If it's the same, ignore it. If it's different, fire off your Ajax request and update the value of "lastSelected".
没有找到我喜欢的解决方案或与我的风格完全一致的解决方案,因此决定捕获“change”和“keyup”事件的触发器,以避免这种情况。
Did not find a solution that I liked or worked clean with my style so decided to catch trigger on both the 'change' and 'keyup' events so as to avoid this scenario.