2016-03-08 44 views
0

因此,我在我的程序中使用JQuery滑块https://jqueryui.com/slider/,并试图释放滑块触发一个更新数据库的事件。我有什么是我怎样才能使一个jQuery UI滑块点击后检测鼠标

$('.ui-slider-handle').mouseup(function() { 
    var $span = $(this).closest('#sliders').find('span.value'); // the span element that holds the value associated with the slider that was clicked 
    $.ajax({ 
     method: 'POST', 
     url: '/UpdateAnswer', 
     data: { 
      Answer: { 
       QuestionId: $span.attr('data-qstnid'), 
       AnswerId: $span.attr('data-answid'), 
       AnswerVal: parseInt($span.text(), 10) 
      }, 
      pid: $('input[name="pid"]').val() 
     } 
    }); 
}); 

,这工作得很好,除了在1型意想不到的情况:当按住滚动条上的鼠标按钮,移动滑块,然后松开鼠标之前移动他们的鼠标关闭滑块。在这种情况下,滑块的$span未定义。

解决此问题的最佳方法是什么? JavaScript/jQuery是否有这样做的自然方式?或者我将不得不创建一个mousedown处理程序,它将元素传递给一些其他函数,然后附加一个mouseup处理程序......或类似的东西?

让我知道是否需要更详细地描述我的情况。

回答

0

您应该使用jQueryUI事件stop,他们已经为您解决了这个问题。从documentation

$(".selector").on("slidestop", function(event, ui) {}); 
0

而不是使用一个mouseup事件,你也许应该使用滑块本身的changestop事件。

$(".selector").on("slidechange", function(event, ui) {}); 

$(".selector").on("slidestop", function(event, ui) {}); 
+0

不知道这些.... –

+0

什么是'.selector'应该被选择? –

+0

您创建UI滑块的元素。创建滑块时,您还可以添加事件处理程序作为和选项。有关示例,请阅读链接的API文档。 – jmoerdyk