jQuery Mobile: how to get the change event of a range Slider?

后端 未结 2 1568
走了就别回头了
走了就别回头了 2020-12-21 20:27

What I am trying to do is just like the code: when the value is greater than 10, display >10, or display 1 - 10. It works fine. However, if I uncomment the jquery mobile , i

相关标签:
2条回答
  • 2020-12-21 20:38

    I would suggest you to wrap input within a div and attach change event to that particular div, through which you can get the value of slider. Working Snippet below.

    $("#slider").change(function() {
      $("#display").text($('#range').val()<=10?'1-10':'>10');
    });
    <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.css">
    <script src="https://code.jquery.com/jquery-1.11.3.min.js"></script>
    <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.min.js"></script>
    <form>
      <div id="slider">
        <label for='range'>as</label>
    
        <input id='range' type='range' min="0" max="100" value="0">
      </div>
      <p id='display'>display</p>
    </form>

    0 讨论(0)
  • 2020-12-21 20:56

    There are three JQM slider events available after widget initialization:

    • slidestart
    • change
    • slidestop

    To get the correct behavior, you should provide the correct JQM page structure and register the event handlers inside the pagecreate event. Below is a simple stub where you can test a JQM slider:

    $(document).on("pagecreate", "#page-one", function() {
      $("#slider").on("slidestart", function() {
        var val = $(this).val();
        $("#txt1").val(val);
      });
      $("#slider").on("change", function() {
        var val = $(this).val();
        $("#txt2").val(val);
      });
      $("#slider").on("slidestop", function() {
        var val = $(this).val();
        $("#txt3").val(val);
      });
    });
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
      <link rel="stylesheet" href="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.css">
      <script src="https://code.jquery.com/jquery-1.11.2.min.js"></script>
      <script src="https://code.jquery.com/mobile/1.4.5/jquery.mobile-1.4.5.js"></script>
    </head>
    <body>
      <div id="page-one" data-role="page">
        <div role="main" class="ui-content">
          <label for="slider">Slider</label>
          <input type="range" name="slider" id="slider" min="1" max="100" value="1" step="1" autocomplete="off" />
            <fieldset class="ui-grid-b">
              <div class="ui-block-a">
                <label for="txt1">slidestart:</label>
                <input data-mini="true" name="txt1" id="txt1" value="" type="text">
              </div>
              <div class="ui-block-b">
                <label for="txt2">slidechange:</label>
                <input data-mini="true" name="txt2" id="txt2" value="" type="text">
              </div>
              <div class="ui-block-c">
                <label for="txt3">slidestop:</label>
                <input data-mini="true" name="txt3" id="txt3" value="" type="text">
              </div>
            </fieldset>
        </div>
      </div>
    </body>
    </html>

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