我有很多时间序列数据,我想创建一个滑块来更新D3中的图形。我已经制作了各种滑块来进行练习,但是,没有一个滑块与变量绑定或完全停止程序。我正在尝试实现一个here。我现在为此的一小段代码是:
<input type = "range" min="0" max="1000" value="0" step="1" onChange="something"/>
<script>
var ARRAY_INDEX = 0; // I want the slider to update this variable
</script>
ARRAY_INDEX在要使用的JSON元素数组中选择一个特定的索引。我希望能够更改此值,而不必自己返回代码并自己更改数字。
最佳答案
我在滑块上添加了id
:
<input type = "range" min="0" max="1000" value="0" step="1" id="slider"/>
使用
id
选择滑块元素的脚本:var ARRAY_INDEX = 0; // You better declare this var outside the event handler function if you want to use it else where
$("#slider").on("change",function(){
ARRAY_INDEX = $(this).val();
console.log(ARRAY_INDEX)
});
Demo
就像dandavis在下面的评论中所说,您也可以使用
input
事件而不是change
。这取决于您是要在光标移动时(实时值)还是仅在“释放”光标(最终值)时更新变量。
关于javascript - 如何将滑块绑定(bind)到Javascript中的变量?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38383203/