简体   繁体   English

防止mouseup事件多次触发

[英]preventing a mouseup event triggering multiple times

I have read several previous questions about similar issues and already think I've gathered that it's a problem with propagation through multiple DOM items, however I'm still finding that my Ajax gets fired multiple times when the mouseup even triggers after releasing my jquery slider. 我已经阅读了有关类似问题的先前几个问题,并且已经认为我已经收集到了通过多个DOM项传播的问题,但是我仍然发现,当释放我的jquery滑块后,甚至在mouseup触发时,我的Ajax也会被触发多次。

The code I've experimented with is below. 我尝试过的代码如下。 Can anyone see anything glaringly obvious and explain why the propagation statement I included isn't working? 谁能看到明显的东西并解释为什么我包含的传播语句不起作用? I've tried the e.stopPropagation(); 我已经尝试过e.stopPropagation(); line in multiple places. 在多个地方排队。

        $('#slider-range').mousedown(function(e) {

                $('#slider-range').one('mouseup', function() {
                        e.stopPropagation();        
                    $("#"+pupil).css("visibility","hidden");
                    $( "img[name='save"+pupil+"']" ).show();


                                $.ajax(
                                {
                                    type: "POST", 
                                    url: "readingAgeAjax.php",
                                    data: (
                                    {
                                        "type": "saveReadingAge",
                                        "readingAge": $('#hiddenSpan'+pupil).html(),
                                        "pupilID": pupil
                                    }), 

                                    success: function(response)
                                    {   
                                        $( "img[name='save"+pupil+"']" ).hide();                
                                    }
                                });




                        });
                     return false;
                });

It's not clear how you want to use your slider. 目前尚不清楚您要如何使用滑块。 You basically indicated you wanted to do this: 您基本上表示您想这样做:

 $(function() { $('#slider-range').mousedown(function(e) { var myEvent = e; console.log("mousedown", myEvent); $('#slider-range').one('mouseup', function(e) { myEvent.stopPropagation(); console.log("mouseup", e); }); return false; }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="slidecontainer"> <input type="range" min="1" max="100" value="50" class="slider" id="slider-range"> </div> 

This would suggest you want to stop the mousedown event during the mouseup event. 这建议您要在mouseup事件期间停止mousedown事件。 Not sure how this would help. 不确定这将如何帮助。

Since you need to send and receive data via AJAX, I would wait until the user has completed the slide event and then send the value via AJAX. 由于您需要通过AJAX发送和接收数据,因此我将等到用户完成幻灯片事件,然后再通过AJAX发送该值。 So either looks for slide-stop, change, or mouseup and then send the data. 因此,要么寻找滑行停止,更改或鼠标悬停,然后发送数据。

 $(function() { $('#slider-range').on("mousedown mouseup", function(e) { console.log(e); if (e.type == "mouseup") { console.log($(this).val()); } }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="slidecontainer"> <input type="range" min="1" max="100" value="50" class="slider" id="slider-range"> </div> 

I think you have missed adding the e in function parameter 我认为您错过了在功能参数中添加e

$('#slider-range').one('mouseup', function(e) { $('#slider-range')。one('mouseup',function(e){

Also check your console for errors 同时检查您的控制台是否有错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM