如何(动态)将大量滑块连接到输入字段?
因为这个页面似乎是参考(除了官方 jquery 文档),当涉及到高级 jquery 问题时,我将在这里发布我的问题。我有一个 ajax 表单,最多有 80 个(!)滑块,基本上工作正常。不过,有一些滑块的范围从 0 到 500,这是很多步骤,特别是滑块的高度只有 90 像素左右。你可以想象,将滑块移动到某个值是可以退出的,有时几乎是不可能的。
所以我想另外提供通过输入字段输入值的机会。您知道如何实现这一点,甚至不需要生成 80 个输入吗?我的想法是这样的,单击特定的句柄,输入字段将获得传输到的值。这应该可以正常工作,但是如何“反转”连接,以便如果您在输入字段中输入一个值,它将被传输到句柄?
提前感谢,这是一个很棒的网站...
Maschek
since this page seems to be THE reference (besides the official jquery documentation), when it comes to advanced jquery questions, i gonna post my question here. I have an ajax-form with up to 80(!) sliders, basically its working fine. there are some sliders though, having a range from 0 to 500, which is a lot of steps, especially, as the height of the slider is only about 90px. You can imagine, that it can be quit an act to move the slider to a certain value, sometimes its nearly impossible.
So i want to additionally giving the opportunity to enter the values via input field. Do you have any idea, how to realize this, maybe even without having to generate 80 inputs? my idea would be something like, clicking the particular handle and the input-field will get the value transmitted to. This should work without problems, but how to have the connection "reversed", so if you enter a value into the input-field, it will be transmitted to the handle?
Thanx in advance, this is a great site...
Maschek
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
.change()
docs 输入框的事件,并调用value
docs 滑块的方法来更新它。示例 http://jsfiddle.net/gaby/MXZGE/ (黄色值是可更新的)
You can use the
.change()
docs event of the input box, and call thevalue
docs method of the slider to update it.example http://jsfiddle.net/gaby/MXZGE/ (the yellow value is updatable)
这是我不久前针对这种情况开发的一些代码。我在输入中有一个名为“data-control-by”的属性,该属性设置为与初始化滑块的 div 相同的值。例如,我的 html 中有这个:
在我的 js 中,我首先像这样初始化滑块:
注意上面的
sliderUpdateInput
回调函数,在幻灯片中使用(当用户滑动时)和停止(当用户停止滑动时)事件。其外观如下:然后,在要更新的输入上,添加一个 keyup 处理程序,如下所示:
更新:进一步说明
请注意此处检查的上/右和下/左键码。这样用户只需向任一方向将滑块微移 1 即可。当输入为 3 位数值(例如 255)时,这特别有用。
我真的希望这能帮助您并促进您的学习!有任何问题请评论!在 keyup 事件处理程序的选择器中,您可以指定任何内容,以便获得您希望由滑块控制/可以控制滑块的所有输入。
Here's some code I developed a little while ago for just this situation. I have an attribute in the input called 'data-controlled-by' that is set to the same value as the div in which the slider is initialized. For instance, I have this in my html:
In my js, I'd initialize the slider first like this:
Notice the
sliderUpdateInput
callback function above, used in both the slide (while the user is sliding) and the stop (when the user stops sliding) events. Here's how that looks:Then, on the input that you want to be updated, you'd add a keyup handler, like this:
Update: further explanation
Notice the keycodes that are checked here for up/right and down/left. This is so the user can just nudge the slider by 1 in either direction. This is especially useful for when the input is at a 3-digit value, like 255.
It's truly my hope that this helps you out and spurs your learning! Comment with any questions, please! In the selector of the keyup event handler, you can specify anything that will get you all of your inputs that you wish to be controlled by/can control the sliders.
我做了一些小的修改,现在它可以为所有滑块使用一个输入字段。这就是我所做的:
在函数“sliderUpdateInput”中,我添加了行“document.getElementById("whatever").setAttribute("data-control-by", this.id);”。然后在输入字段中添加 id="whatever"。现在唯一需要注意的是,滑块属性“data-control-by”中的值与滑块的 id 相同,因此您可以使用 this.id 在函数“sliderUpdateInput”中应用它。因此,每次单击或拖动滑块时,输入属性的值都会更改为相应的滑块。
如果你想显示每个滑块的所有输入,你不需要这个,但在我的脚本中没有空间容纳所有这些输入,所以我认为最好只使用一个输入,让它有点更有活力。
感谢您的解决方案,我特别喜欢输入值与滑块实时对应的方式。
i did some minor modifications, now its working with one single input-field for all sliders. Heres what i did:
In the function "sliderUpdateInput" i added the line "document.getElementById("whatever").setAttribute("data-controlled-by", this.id);". then to the input field added the id="whatever". Now only thing to take care is, that in the sliders attribute "data-controlled-by" the value is the same as the id of the slider, so you can apply to it in the function "sliderUpdateInput" with this.id. So the value of the input-attribute will be changed to the corresponding slider, each time you click or drag the slider.
If you want to display all the inputs for each slider anyway, you wont need this, but in my script there was no room for all that inputs, so i thought it was better, to only use one input anyway, to have it a bit more dynamic.
Thanks for your solution, i especially like how the value of the input corresponds with the slider in realtime.