【问题标题】:jQuery Mobile: how to get the change event of a range Slider?jQuery Mobile:如何获取范围滑块的更改事件?
【发布时间】:2017-06-13 07:45:35
【问题描述】:

我要做的就像代码一样:当值大于10时,显示>10,或者显示1-10。它工作正常。但是,如果我取消注释 jquery mobile ,它就不再起作用了。

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<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>-->

</head>
<body>
<form>
<label for='range'>as</label>
<input id='range' type='range' onmousemove='test()'>
<p id='display'>display</p>
</form>
<script>
function test(){
  var v = $('#range').val();
  if(v <= 10)$("#display").text('1-10');
  else $("#display").text('>10');
}
</script>
</body>
</html>

【问题讨论】:

  • 您在控制台中看到任何特定错误?
  • 试过了,对我有用吗?真正的问题是什么?
  • 如果你取消注释第 7 行

标签: jquery jquery-mobile


【解决方案1】:

小部件初始化后有三个可用的 JQM 滑块事件:

  • 幻灯片开始
  • 改变
  • 滑动停止

要获得正确的行为,您应该提供正确的 JQM 页面结构并在 pagecreate 事件中注册事件处理程序。下面是一个简单的存根,您可以在其中测试 JQM 滑块:

$(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>

【讨论】:

    【解决方案2】:

    我建议您将input 包装在div 中,并将change 事件附加到特定的div,通过它您可以获得slider 的值。下面的工作片段。

    $("#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>

    【讨论】:

    • 非常感谢!这是一个很好的解决方案。但我仍然想知道为什么在包含 jquery mobile 后它不起作用的原因。 jquery mobile 是否更改了 jquery 的默认事件??
    • jquery mobile 可能会添加自己的元素版本以使其 ui 和其他东西正常工作。它隐藏默认的 input 元素并使用 divsspans 呈现其新控件等等,事实上所有的插件都会做同样的事情。所以,默认的东西可能不起作用,我们需要选择他们的方式来做这件事。:)
    • @GuruprasadRao:如果问题不包括 JQM,你的回答会很好
    • @deblocker 同意.. 我支持你.. 我要删除这个答案,因为你的似乎是实际的解决方案.. 但我要删除,OP 必须不接受我的答案..跨度>
    • @GuruprasadRao:如果您以更简单的方式在您的答案中发布 sn-p,那就太好了,我真的很喜欢它作为一个教学解释如何以非 JQM 方式做同样的事情. (我相信downvote只是因为你的第一句话)
    猜你喜欢
    • 2012-02-12
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-27
    相关资源
    最近更新 更多