防止其他链接在 AJAX 调用期间工作
我有一个包含多个链接的页面,每个链接执行不同的 AJAX 调用,然后在页面的另一个区域动态插入一些数据。当用户单击这些链接中的任何一个时,我想禁用所有其他链接,直到调用的数据返回,这样他们就不能同时发出多个请求。
这些链接都位于同一个
中,因此我可以轻松定位所有这些链接,而无需触及页面上其他位置的其他“常规”链接。最好的方法是什么?
I have a page with multiple links that each perform different AJAX calls and then dynamically insert some data in another area of the page. When the user clicks on any one of these links, I want to disable all the others until the data from the call comes back, so they can't make multiple requests at once.
The links are all located in the same <div>
, so I can easily target just all of those without touching other "regular" links elsewhere on the page.
What would be the best way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当单击 A 类的某些内容时,如何阻止 B 类链接上的默认行为?
HTML:
脚本:
当您希望允许对 linkTypeB 的点击重新开始工作时,您需要取消绑定对 linkTypeA 的点击事件。
如果您不想使用类,则可以检查单击的源元素并阻止除源元素之外的所有标记上的默认行为。
How about preventing the default behaviour on links that have class B, when something of class A is clicked?
HTML:
Script:
You'll need to unbind the click event on linkTypeA when you want to allow clicks on linkTypeB to start working again.
If you didn't want to use classes, you could check the source element of the click and prevent the default behaviour on all tags other than the source element.
实现此目的的方法之一是显示具有高 z-index 的半透明图像。图像可以覆盖您的 div。这样,所有内容仍然可见,但不可点击。
One of the ways to do this is to display a translucent image with high z-index. Image can cover your div. This way all is still visible but non-clickable.
我认为最简单的方法是每当单击链接时,在页面顶部创建一个大 div 并显示加载图标,以便用户可以单击该 div 下面的任何内容。然后在ajax调用结束后隐藏加载div。
您可以使用已经制作的加载插件,例如 jquery showloading
只需要做
,并且在 ajax 调用之后
它也有其他功能
I think the easiest way is whenever click a link, create a big div on top the page and showing a loading icon, so user can click anything below that div. and then hide the loading div after ajax call ends.
you can use already made loading plugins like jquery showloading
just have to do
and after ajax call
it also has other features
您可以在 ajax 运行时使用阻止默认值,例如在
beforeSend
上:you could use prevent default while ajax is running, say on
beforeSend
: