admin管理员组文章数量:1433161
I am trying to change the value of a range slider using the mousewheel anywhere on a page, but I don't know how to programmatically change the value of the input.
Here is my code:
HTML:
<input id="slider" type="range" min="0" max="100" step="1" value="50">
Javascript:
$(window).mousewheel(function(event, delta){
$('#slider').val($('#slider').val()*1 + delta);
});
// Another thing I tried, but didn't work:
/*
$('#slider').attr('value', oldval + 1);
*/
Any pointers are appreciated.
I am trying to change the value of a range slider using the mousewheel anywhere on a page, but I don't know how to programmatically change the value of the input.
Here is my code:
HTML:
<input id="slider" type="range" min="0" max="100" step="1" value="50">
Javascript:
$(window).mousewheel(function(event, delta){
$('#slider').val($('#slider').val()*1 + delta);
});
// Another thing I tried, but didn't work:
/*
$('#slider').attr('value', oldval + 1);
*/
Any pointers are appreciated.
Share Improve this question edited Jan 15, 2014 at 18:10 Krease 16.2k8 gold badges57 silver badges92 bronze badges asked Jan 28, 2011 at 19:49 lowerkeylowerkey 8,36518 gold badges70 silver badges102 bronze badges 8-
It works fine here (Google Chrome 9.0.597.83), but you should consider checking if the input has focus, by changing
$(window)
to$('#slider')
– user216441 Commented Jan 28, 2011 at 19:52 - @Joshua thanks for the tip! good idea to move the slider with the mousewheel! – Caspar Kleijne Commented Jan 28, 2011 at 19:56
- Also, if any browser add native support to that feature, then your solution will fail, try blocking the event too. – user216441 Commented Jan 28, 2011 at 19:57
- Seems to work fine in Firefox 3.6.13. In what browser is it not working? – Scott Cranfill Commented Jan 28, 2011 at 20:09
- @M28 focus doesn't seem to be the issue. I added an alert to the event handler and got the alert... (i'm using chrome as well) – lowerkey Commented Jan 28, 2011 at 20:16
1 Answer
Reset to default 5It is being interpreted as a string. Use this
$(window).mousewheel(function(event, delta){
$('#slider').val($('#slider').val()*1 + delta);
// var oldval = $('#slider').attr('value');
// if(delta > 0)
// document.getElementById('slider').value++;
// else
// document.getElementById('slider').value--;
});
// Another thing I tried, but didn't work:
/*
$('#slider').attr('value', oldval + 1);
*/
本文标签: javascriptHow do I programmatically change the value of ltinput typequotrangequot Stack Overflow
版权声明:本文标题:javascript - How do I programmatically change the value of <input type="range" ...? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745567775a2663865.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论