Jquery Mobile Slider change event

只愿长相守 提交于 2019-11-30 20:09:45

Michael. Try to surround the slider with another element:

<div id="div-slider">
  <input type="range" id="slider-1" />
</div>

And you can get an event on change:

$("#div-slider").change(function() {
  var slider_value = $("#slider-1").val();
  // do something..
});

The problem with your code is that the javascript binding the event of change to the elements is evaluated before they were actually created in the html, thus no element get binded to the change event as you specified.

Try this:

$('#slider-1').live('change', function(){
  doYourStuffHere();
});

Briefly, live means that JQuery will keep on binding events to elements matching the specified selector even if they were not present at time of evaluating the JS for binding, so this code will bind all present and future html elements which match #slider-1 selector with the proper change callback.

Another alternative is to use JQuery 'on' binding which acts little bit differently, but can still do the job. Have a look at the documentation for 'on' here.

I highly suggest checking out w3schools tutorial on this.

http://www.w3schools.com/jquerymobile/jquerymobile_events_intro.asp

The crucial piece of code that you are missing is:

$(document).on("pageinit","#pageone",function(){
    // PLACE ALL YOU EVENTS CODE HERE e.g.
    $( "#slider" ).on( 'slidestop', function( event ) { 
        alert ('hi');  
    });
}

Make sure your content is prefixed with:

<div data-role="page" id="pageone">
<div data-role="content">
<input type="range" name="slider" id="slider" value="10" min="0" max="100">
</div>
</div>
nskeskin

Use this code,

$( ".mySliders" ).slider({
    create: function (event, ui) {
        $(this).bind('change', function () {

        });
    }
});

Do not put type="range" to your input tags, put type="text" instead.

Since you are calling slider function manually.

The documentation appears to be lacking. After searching for a long time I found that to get the new value in the event handler, something like $(this).slider().val() is needed (the extra slider() being the part omitted almost everywhere):

$("#div-slider").change(function() {
  var slider_value = $(this).slider().val();
  // do something..
});

I had the same problem, too many troubles with the event of the slider. Finally I found 'slidestop' event that works great. This is the code:

$( "#slider-1").on('slidestop', function( event ) {
   var slider_value=$("#slider-1").slider().val();
   alert('Value: '+slider_value);
});

I hope this work for you

I'm not too familiar with jquery mobile, but adding a change handler to the original input element should do:

$('#slider-1').change(function(){
    var slider_value = $(this).val()
    console.log(slider_value)
    // do whatever you want with that value...
})

Hope this help,

Martin

try this:

var self = this;
this.sliderTouchUp = function() {
    var currentVal = $('#slider-1').val();
    console.log("val change to " + currentVal);
};
$('.ui-slider').live('mouseup', self.sliderTouchUp);
$('.ui-slider').live('touchend', self.sliderTouchUp);

Shortest yet and functional.

$('body').change(function() {
    var slider_value = $('#slider-1').val();
    if(slider_value == 4000){   
        alert("Holy smoke!");
    }
}

i'm using this code and it works in jquery mobile 1.4.3.

$(".ui-slider").on('change', function(event) { 
    var valuenow = $(this).find(".ui-slider-handle").attr("aria-valuenow");
    console.log(valuenow);
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!