Jquery滑块(查询中的php值)

时间:2016-02-21 21:18:00

标签: javascript php jquery

需要你的Jquery滑块帮助。

我的jquery代码

<script>
jQuery(document).ready(function($) {
      $('#price_filter').val(<?php echo $min; ?>-<?php echo $max; ?>);
      $("#price_slider").slider({
        min: <?php echo $min; ?>,
        max: <?php echo $max; ?>,
        values:[<?php echo $min; ?>, <?php echo $max; ?>],
        step: 100,
        range:true,
        slide: function(event, ui) {
          $("#price_range_label").html('$' + ui.values[ 0 ] + ' - ' + ui.values[ 1 ] + '$' );
          $('#price_filter').val(ui.values[0] + ' -' + ui.values[1]).trigger('change');
        }
      });

      $('#status :checkbox').prop('checked', false);
      $('#all_status').on('click', function(){
        $('#status :checkbox').prop('checked', $(this).is(':checked'));
      });

      FilterJS(services, "#service_list", {
        template: '#template',
        criterias:[
          {field: 'amount', ele: '#price_filter', type: 'range'},
          {field: 'status', ele: '#status :checkbox'}

        ],

      });
});

</script>

HTML

<center><span id="price_range_label" style="margin:10px;">
<?=$min; ?>-<?=$max; ?> USD.</i></span></center>
<br/>
<div id="price_slider"></div>
<input type="hidden" id="price_filter" value='<?php echo $min; ?>-<?php echo $max; ?>'/>

问题是,当我选择最大范围并将其滑动到最小值时,某个地方已经消失了。只有当我从最小值滑动到最大值时,此滑块才能正常工作。

此外,我有一个巨大的值(例如最小500最大10000000)

我做错了什么?任何人都可以帮我解决吗? :(

2 个答案:

答案 0 :(得分:1)

不要在jQuery中使用PHP代码。您可以将值设置为jQuery,例如$(element).attr('attribute');$(element).val();

您只需写入HTML:

<input type="hidden" id="min" value="<?p=$min;?>">
<input type="hidden" id="max" value="<?p=$max;?>">

在jQuery中:

min: $('#min').val(),
max: $('#max').val(),
values:[$('#min').val(), $('#max').val()],

答案 1 :(得分:0)

使用jQuery插件进行滑块功能的一个很好的替代方法是使用原生支持进行范围输入。它受 (FF23+, Chrome 4+, Safari 3.1+, Opera 10+, Android 2.1 and IE10+) 支持。

您可以设置样式并使用jQuery进行事件监听。

以下是一个例子:

$('.slider').on('change input', function() {
  $('.text').text($(this).val());
});
input[type=range] {
  -webkit-appearance: none;
  margin: 10px 0;
  width: 100%;
}

input[type=range]:focus {
  outline: none;
}

input[type=range]::-webkit-slider-runnable-track {
  width: 100%;
  height: 12.8px;
  cursor: pointer;
  animate: 0.2s;
  box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
  background: #ac51b5;
  border-radius: 25px;
  border: 0px solid #000101;
}

input[type=range]::-webkit-slider-thumb {
  box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
  border: 0px solid #000000;
  height: 20px;
  width: 39px;
  border-radius: 7px;
  background: #65001c;
  cursor: pointer;
  -webkit-appearance: none;
  margin-top: -3.6px;
}

input[type=range]:focus::-webkit-slider-runnable-track {
  background: #ac51b5;
}

input[type=range]::-moz-range-track {
  width: 100%;
  height: 12.8px;
  cursor: pointer;
  animate: 0.2s;
  box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
  background: #ac51b5;
  border-radius: 25px;
  border: 0px solid #000101;
}

input[type=range]::-moz-range-thumb {
  box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
  border: 0px solid #000000;
  height: 20px;
  width: 39px;
  border-radius: 7px;
  background: #65001c;
  cursor: pointer;
}

input[type=range]::-ms-track {
  width: 100%;
  height: 12.8px;
  cursor: pointer;
  animate: 0.2s;
  background: transparent;
  border-color: transparent;
  border-width: 39px 0;
  color: transparent;
}

input[type=range]::-ms-fill-lower {
  background: #ac51b5;
  border: 0px solid #000101;
  border-radius: 50px;
  box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
}

input[type=range]::-ms-fill-upper {
  background: #ac51b5;
  border: 0px solid #000101;
  border-radius: 50px;
  box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
}

input[type=range]::-ms-thumb {
  box-shadow: 0px 0px 0px #000000, 0px 0px 0px #0d0d0d;
  border: 0px solid #000000;
  height: 20px;
  width: 39px;
  border-radius: 7px;
  background: #65001c;
  cursor: pointer;
}

input[type=range]:focus::-ms-fill-lower {
  background: #ac51b5;
}

input[type=range]:focus::-ms-fill-upper {
  background: #ac51b5;
}

body {
  padding: 30px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input class='slider' type="range" min="50" max="500000" value="100" />
<div class='text'>
50
</div>

这是一个jsfiddle: https://jsfiddle.net/nu2m0cm9/