设置jquery移动滑块会导致错误

问题描述 投票:0回答:2

我试图在jQuery mobile 1.4.5,JQ 2.1.4中设置滑块的值,并且确实收到此错误:

错误:滑块小部件实例没有这样的方法'值'

HTML代码:

<div data-role="rangeslider" id="filter_price" style="touch-action: none;">
    <input type="range" name="price_from" id="price_from" min="0" max="30000" step="250"  data-popup-enabled="true">
    <input type="range" name="price_to" id="price_to" min="0" max="30000" step="250"  data-popup-enabled="true">
</div>

jQuery的:

   $('#filter_price').slider();
   $('#filter_price').slider('values', 0,  50);  
   $('#filter_price').slider('values', 1,  100);  

这看起来与几年前的the answer provided on SO完全一样,但可能已经过时,因为它来自8岁。

jquery jquery-mobile
2个回答
2
投票

这里有几个问题。首先,你在range中创建了一些div输入,你正在实例化滑块。这只会导致问题,因为它们会干扰滑块布局。我建议删除它们。

其次,使用两个参数调用values旨在与范围滑块一起使用,而不是单个值滑块。我假设您在DOM中的两个范围输入中尝试将其作为范围选择。因此,您需要在创建滑块时在滑块上设置range: true。然后你可以像现在这样打电话给values。试试这个:

$('#filter_price').slider({
  range: true,
  min: 0,
  max: 150
});
$('#filter_price').slider('values', 0, 50);
$('#filter_price').slider('values', 1, 100);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" />
<div data-role="rangeslider" id="filter_price" style="touch-action: none;">
</div>

另请注意,如果您尝试使用这些值创建滑块,则可以在实例化对象中提供所有参数,而无需再次选择该元素:

$('#filter_price').slider({
  range: true,
  min: 0,
  max: 150,
  values: [50, 100]
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.0/jquery-ui.min.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/smoothness/jquery-ui.css" />
<div data-role="rangeslider" id="filter_price" style="touch-action: none;">
</div>

0
投票

我对派对来说有点太晚了,但这里是jQuery Mobile 1.4.5 rangeslider(不是jQuery-UI)的解决方案。

由于JQM rangeslider有效地构建在两个JQM滑块之上,您可以在这两个min事件中以编程方式设置maxcreate值:

var range = {
  "low": {min: 1, max: 10000}, 
  "medium": {min: 10000, max: 20000}, 
  "high": {min: 20000, max: 30000}
};

$(document).on("slidecreate", "#price_from", function(e) {
  $(this).val(range["medium"].min);
});

$(document).on("slidecreate", "#price_to", function(e) {
  $(this).val(range["medium"].max);
});

$(document).on("rangeslidercreate", "#filter_price", function(e) {
  // rangeslider has been created
});

$(document).on("change", "input:radio[name='price-range']", function() {
  $("#price_from").val(range[this.value].min);
  $("#price_to").val(range[this.value].max);
  $("#filter_price").rangeslider("refresh");
});
.ui-slider-popup {
  width: auto !important;
  padding: 14px 8px 0 !important;
}

.ui-grid-a .ui-block-a,
.ui-grid-a .ui-block-b {
  text-align: center;
}
<!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 data-role="header">
      <h3>Header</h3>
    </div>
    <div role="main" class="ui-content">
      <br>
      <div class="ui-grid-a">
        <div class="ui-block-a">
          <div data-role="rangeslider" id="filter_price" style="touch-action: none;">
            <input type="range" name="price_from" id="price_from" min="0" max="30000" step="250"  data-popup-enabled="true">
            <input type="range" name="price_to" id="price_to" min="0" max="30000" step="250"  data-popup-enabled="true">
          </div>
        </div>
        <div class="ui-block-b">
          <fieldset data-role="controlgroup" data-type="horizontal" data-mini="true">
            <input name="price-range" id="price-range-low" value="low" type="radio">
            <label for="price-range-low">Low</label>
            <input name="price-range" id="price-range-medium" value="medium" type="radio" checked="checked">
            <label for="price-range-medium">Medium</label>
            <input name="price-range" id="price-range-high" value="high" type="radio">
            <label for="price-range-high">High</label>
          </fieldset>
        </div>
      </div>
    </div>
  </div>
</body>
</html>
© www.soinside.com 2019 - 2024. All rights reserved.