我想在用户更改 JQuery 范围控件上的值时更新一个值。我试着按照给定的例子 here ,但是它不起作用。
$(document).ready(function () {
alert('ready1'); //This fires when the page loads.
// Logs the value while the user is moving the slider
$('.price-min').on('input', getValue);
function getValue(e) { //This never fires
var val = $(e.element).val();
alert(val);
}
});
HTML:
<div data-role="rangeslider">
<input type="range" name="price-min" id="price-min" value="200" min="0" max="1000">
<input type="range" name="price-max" id="price-max" value="800" min="0" max="1000">
</div>
有人能告诉我哪里错了吗?
最佳答案
您的代码有几件事。
- 您正在按类别选择元素 (
$('.price-min')
),但您的范围有一个 ID ($('#price-min)'
). - 监听
change
事件。
$(document).ready(function () {
function getValue(e) { // now this fires on range change event
var val = $(this).val();
console.log(val);
}
// Logs the value while the user is moving the slider
$('#price-min').on('change', getValue);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div data-role="rangeslider">
<input type="range" name="price-min" id="price-min" value="200" min="0" max="1000">
<input type="range" name="price-max" id="price-max" value="800" min="0" max="1000">
</div>
关于javascript - JQuery 范围事件未触发。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46779639/