【发布时间】:2019-03-28 20:34:07
【问题描述】:
我有一些带有 input[type="text"] 和 input[type="range"] 的 div。
<div class="box">
<input type="text" value="10">
<input type="range" min="0" value="0" max="18" step="1">
</div>
<div class="box">
<input type="text" value="10">
<input type="range" min="0" value="0" max="18" step="1">
</div>
在一些 jQuery 的帮助下,范围滑块的步长不均匀,并且文本输入字段的值在移动滑块时会发生变化。
如果您在输入字段中输入数字,该数字将更改为最接近的步长/值。
stepVal = [10, 35, 50, 90, 100, 135, 155, 170, 190, 220, 230, 250, 270, 290, 310, 320, 350, 365, 400];
$('.box').each(function() {
$(this).on('input', 'input[type="range"]', function() {
$(this).prev('input[type=text]').val(stepVal[this.value]);
});
$(this).on('blur', 'input[type="text"]', function() {
var textVal = $(this).val();
var closestVal = stepVal.reduce(function(prev, curr) {
return (Math.abs(curr - textVal) < Math.abs(prev - textVal) ? curr : prev);
});
$(this).val(closestVal);
$(this).next('input[type=range]').val(closestVal);
});
});
问题是范围滑块仅移动到末尾或中间,而不是输入的步长/值。任何想法如何解决这个问题?
【问题讨论】:
标签: javascript jquery html input html-input