我需要一个带有两个句柄的jquery滑块,其中包含来自两个文本框的输入,例如http://www.israel-diamonds.com/search/diamonds/default.aspx。目前我有一个单一的句柄滑块,来自单个文本框的输入
$("#slider").slider({
value: 1,
step: 1000,
min: 0,
max: 5000000,
slide: function(event, ui) {
$("input").val("$" + ui.value);
}
});
$("input").change(function () {
var value = this.value.substring(1);
console.log(value);
$("#slider").slider("value", parseInt(value));
});
有什么建议吗?
编辑:
<div class="demo">
<input type="text" class="sliderValue" />
<p>
</p>
<div id="slider"></div>
</div>
和
$("#slider").slider({
range: "min",
value: 1,
step: 10,
min: 0,
max: 1000,
slide: function (event, ui) {
$("input").val(ui.value);
}
});
$("input").change(function (event) {
var value1 = parseFloat($("input").val());
var highVal = value1 * 2;
$("#slider").slider("option", { "max": highVal, "value": value1 });
});
答案 0 :(得分:13)
假设您有两个<input>
元素:
<input type="text" class="sliderValue" data-index="0" value="10" />
<input type="text" class="sliderValue" data-index="1" value="90" />
滑块占位符元素:
<div id="slider"></div>
您可以使用values
选项将滑块窗口小部件置于多个句柄模式,并将<input>
元素的值与:
$("#slider").slider({
min: 0,
max: 100,
step: 1,
values: [10, 90],
slide: function(event, ui) {
for (var i = 0; i < ui.values.length; ++i) {
$("input.sliderValue[data-index=" + i + "]").val(ui.values[i]);
}
}
});
$("input.sliderValue").change(function() {
var $this = $(this);
$("#slider").slider("values", $this.data("index"), $this.val());
});
您可以在this fiddle中看到结果。
答案 1 :(得分:0)
来自官方jQuery UI文档:https://jqueryui.com/slider/#range
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Slider - Range slider</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.css">
</head>
<body>
<p>
<label for="amount">Price range:</label>
<input type="text" id="amount" readonly style="border:0; color:#f6931f; font-weight:bold;">
</p>
<div id="slider-range"></div>
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#slider-range" ).slider({
range: true,
min: 0,
max: 500,
values: [ 75, 300 ],
slide: function( event, ui ) {
$( "#amount" ).val( "$" + ui.values[ 0 ] + " - $" + ui.values[ 1 ] );
}
});
$( "#amount" ).val( "$" + $( "#slider-range" ).slider( "values", 0 ) +
" - $" + $( "#slider-range" ).slider( "values", 1 ) );
} );
</script>
</body>
</html>