Jquery Mobile Slider change event

后端 未结 10 1836
独厮守ぢ
独厮守ぢ 2021-01-02 11:14

I have a jquery mobile slider on a simple page. When I drag the slider, the value updates as expected in the textbox. I have looked but cannot find where this logic is happe

相关标签:
10条回答
  • 2021-01-02 11:39

    Shortest yet and functional.

    $('body').change(function() {
        var slider_value = $('#slider-1').val();
        if(slider_value == 4000){   
            alert("Holy smoke!");
        }
    }
    
    0 讨论(0)
  • 2021-01-02 11:40

    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>
    
    0 讨论(0)
  • 2021-01-02 11:42

    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..
    });
    
    0 讨论(0)
  • 2021-01-02 11:43

    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.

    0 讨论(0)
提交回复
热议问题