简体   繁体   English

返回jquery-ui滑块上Chaged句柄的索引

[英]Return the index of chaged handle on jquery-ui slider

I have a jquery ui slider: 我有一个jQuery UI滑块:

$( "#slider" ).slider({
    values: [ 10, 25,45,176 ],
    max: 190,
    change: function( event, ui ) {  
        var index = $("#slider span").index(ui.handle);
        $( "#index" ).text( "That was handle index #" + index );
    }
});

I want to return the index of the changed single handle. 我想返回已更改的单个句柄的索引。 But the handle object returns all spans (handles). 但是handle对象返回所有范围(handles)。

How can i do that? 我怎样才能做到这一点?

Inside of the change event function, ui.handle is the element that was changed. change事件函数内部, ui.handle是已更改的元素。 Therefore, use $(ui.handle).index() to access the index of the element. 因此,使用$(ui.handle).index()访问元素的索引。 Note: The index is zero-based. 注意:索引从零开始。

Example Here 这里的例子

$("#slider").slider({
    values: [ 10, 25,45,176 ],
    max:190,
    change: function( event, ui ) {  
        var index = $(ui.handle).index();
        $("#index").text( "That was handle index #" + index );
    }
});

For anyone seeing this after the March 2015 patch that added the handleIndex property to the ui object, if you need the index of the current handle, you can just use, for example: 对于在2015年3月handleIndex属性添加到ui对象中的补丁之后看到此内容的任何人,如果您需要当前句柄的索引,则可以使用,例如:

$("#slider").slider({
    values: [10, 25, 45, 176],
    max: 190,
    change: function(event, ui) {  
        var index = ui.handleIndex;
        $("#index").text("That was handle index #" + index);
    }
});

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

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