[英]Send ajax request on mouseover
I have a set of element with the same class selector. 我有一组具有相同类选择器的元素。 When I enter mouse on this element I want to send Ajax query, but.
当我在此元素上输入鼠标时,我想发送Ajax查询。 When I just
console.log(1);
当我只是
console.log(1);
on mouse enter all fine, but when I send request it rise in some progression, and each next hover send many requests instead of one. 在鼠标上输入一切正常,但是当我发送请求时,它以某种进度上升,并且每个下一个悬停都发送许多请求,而不是一个。 Here is my code:
这是我的代码:
$(document).ajaxComplete(function () {
$('.device_hover').each(function (key, val) {
$(val).mouseenter(function () {
var val = $(this).html();
console.log(1);
$.ajax({
'type': 'POST',
'url': 'handlers/route_request.php',
'dataType': 'html',
'success': function (data) {
console.log(data);
}
});
});
});
});
Can somebody help me? 有人可以帮我吗? Maybe I am doing something wrong?
也许我做错了什么?
I think the best way is to use hover
event for sending ajax request. 我认为最好的方法是使用
hover
事件发送Ajax请求。 This will send only once. 这将仅发送一次。
$(".device_hover").hover(function(){
var val = $(this).html();
console.log(1);
$.ajax({
'type': 'POST',
'url': 'handlers/route_request.php',
'dataType': 'html',
'success': function (data) {
console.log(data);
}
}, function(){
//This function is for unhover.
});
除了使用悬停事件,您还可以使用onmouseenter和onmouseleave事件仅进行一次呼叫。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.