Trigger a jQuery UI slider event

前端 未结 12 2035
南方客
南方客 2020-12-02 22:04

How can I trigger a change event on a jQuery UI slider?

I thought it would be

$(\'#slider\').trigger(\'slidechange\');

but that do

相关标签:
12条回答
  • 2020-12-02 22:31

    The jQueryUI Slider documentation gives the following example for triggering an event:

    $( ".selector" ).slider({
        change: function( event, ui ) {}
    });
    

    and for the event trigger:

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

    this didn't work for me!

    All I had to do to get it working was to change "slidechange" to "change".

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

    Hope this helps the future generations that stumbleupon this problem.

    0 讨论(0)
  • 2020-12-02 22:37

    Try

    $slider = $('#slider');
    $slider.slider('option', 'change').call($slider);
    

    Not ideal but gets you working!

    alt text

    0 讨论(0)
  • 2020-12-02 22:37

    I Used both slidechanged and slide, slide triggers when drag the point, slidechanged triggers after release the mouse button (just like click event)

    var slider1 = $("#slider1").slider({ min: 1, max: 6 });
    //option 1
    slider1.on("slide", function (e, ui) {
    
    });
    //Option 2
    slider1.on("slidechanged", function (e, ui) {
    
    });
    
    0 讨论(0)
  • 2020-12-02 22:40

    Wanted to add a comment on Joey Yore's answer -

    I think it's better the other way round

    $('#slider').bind({
        slidestart  : function(event,ui) {...},
        slidechange : function(event,ui) {...},
        slidestop   : function(event,ui) {...},
        slidecreate : function(event,ui) {...}
    }).slider();
    

    Otherwise some events (namely, 'slidecreate') will be ignored

    0 讨论(0)
  • 2020-12-02 22:41

    If you bind to to the slider's change event like that:

    $('#slider').change(function() {
        alert('action');
    });
    

    Then you can trigger it like:

    $('#slider').trigger('change');
    

    The solution below mentioned by Joey Yore works too, but the downside is that slidechange event is not fired (from my experience) when user changes your slider from UI.

    //TO DEFINE SLIDER AND EVENTS
    $('#slider').slider().bind('slidechange',function(event,ui){...});
    
    //TO TRIGGER EVENT
    $('#slider').trigger('slidechange');
    
    0 讨论(0)
  • 2020-12-02 22:42

    I found it easier to use the 'create' method to call the slide or stop function. Eg, for a slider with a min/max range:

    $('#height').slider({
            ...
            create: function(event, slider){ $( "#height" ).slider( "option", "values", [1100, 1500] ); handleHeightSlide('slide', $( "#height" ));},
            ...
        });
    
    0 讨论(0)
提交回复
热议问题