HTML5 范围的 onChange 事件
目前,我的范围输入上的 onChange 事件在每一步都会触发。
有没有办法阻止此事件触发,直到用户松开滑块?
我正在使用该范围来创建搜索查询。我希望能够在每次表单更改时运行搜索,但在滑块移动的每一步发出搜索请求太多了。
下面是代码:
HTML:
<div id="page">
<p>Currently viewing page <span>1</span>.</p>
<input class="slider" type="range" min="1" max="100" step="1" value="1" name="page" />
</div>
JavaScript:
$(".slider").change(function() {
$("#query").text($("form").serialize());
});
这有帮助吗?
Currently the onChange event on my range inputs is firing at each step.
Is there a way to stop this event from firing until the user has let go of the slider?
I'm using the range to create a search query. I want to be able to run the search every time the form is changed but issuing a search request at each step of the slider's movement is too much.
Here's the code as it stands:
HTML:
<div id="page">
<p>Currently viewing page <span>1</span>.</p>
<input class="slider" type="range" min="1" max="100" step="1" value="1" name="page" />
</div>
JavaScript:
$(".slider").change(function() {
$("#query").text($("form").serialize());
});
Does that help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
用于最终选择的值:
用于获取滑动时的增量值:
Use for final selected value:
Use to get incremental value as sliding:
有点晚了,但前几天我也遇到了同样的问题。这是我使用 jQuery 绑定/触发的解决方案:
现在只需将您的函数绑定到“更改”事件即可。
Bit late, but I had the same problem the other day. Here is my solution using jQuery bind/trigger:
Now just bind your function to the 'changed' event instead.
呸!
使用
onmouseup
事件而不是onChange
Bah!
Use
onmouseup
event Rather thenonChange
一个问题是,据我所知,HTML5 没有定义何时触发
onchange
事件,因此不同浏览器的情况很可能有所不同。您还必须考虑,浏览器实际上不必将input type=range
呈现为滑块。您唯一的选择是必须建立一种机制来确保您的搜索不会被频繁触发,例如,检查搜索当前是否正在运行,如果正在运行则中止,或者确保在某个时间触发搜索。每 x 秒最多一次。
后者的快速示例(只是一个快速破解,未经测试)。
One problem is that AFAIK the HTML5 doesn't define when the
onchange
event is supposed to fire, so it is most likely different from browser to browser. And you also have to consider, that a browser doesn't actually have to render aninput type=range
as a slider.Your only choice is that you have to build in a mechanism to make sure that your search isn't triggered too often, for example, check if a search is currently running and abort if it is, or make sure that searches are triggered at a maximum of every x seconds.
Quick example for the latter (just a quick hack, untested).
纯 JS 在这里:
或
Pure JS here:
or
Gravediggin,但如果您需要它,请检查 js throttle 或 debounce 函数
用法:
代码:
gravediggin but if you need it check js throttle or debounce functions
Usage:
Code:
以下是我用于捕获 html5 范围滑块的“更改事件”的方法:
HTML:
JavaScript:
如果您愿意,您还可以将“点击”事件绑定到范围滑块想要在单击(甚至拖动)时返回其值。把它想象成一个“mouseup”事件。 (我确实尝试过,但在单击滑块后滑块没有停止。)
JavaScript:
附带说明,这会返回一个字符串,因此请确保使用 'parseInt($(this ).value())' 在适当的时候。
希望这有帮助。
Here's what I use for capturing the 'change event' for the html5 range slider:
HTML:
JavaScript:
You can also bind the 'click' event to the range slider if you want to return its value when it has been clicked (or even dragged). Think of it like a 'mouseup' event. (I did try that but the slider didn't stop after I had clicked on the slider.)
JavaScript:
On a side note, this returns a string so make sure you use 'parseInt($(this).value())' when appropriate.
Hope this helps.
我在同一页面中使用多个 HTML5 默认滑块,并进行以下设置:
oninput
事件移动滑块时,页面中的输出标记会更改值change
事件发布后使用最新的 Chrome 进行了测试,并在带有 Node 和 Socket.io 的 Raspberry 上编译良好。
一个简单的 Javascript 代码即可创建侦听器。您可能需要尝试不同的事件,而不是最后一行中的“更改”,看看什么适合您。
I use several HTML5 default sliders in the same page with the following setup:
oninput
eventchange
event is triggered once on releaseTested with the latest Chrome and compiles well on a Raspberry with Node and Socket.io.
A simple Javascript code creates the listeners. You might need to try different events instead of 'change' in the last line to see what fits you.
另一个建议:
another suggest:
您可以尝试使用
blur
事件。当然它也有它的局限性,但这只是另一个建议:)你也可以尝试结合
blur
、onkeyup
和onmouseup
事件来尝试捕获不同的情况:当用户使用键盘箭头进行选择并点击
时blur
,当用户进行选择时onkeyup
使用键盘时将注意力集中在滑块上,而在使用鼠标时则将注意力集中在onmouseup
上。甚至可以只组合onkeyup
和onmouseup
。尽管如此,您仍然必须进行简单的检查该值是否已更改,并仅在发生更改后才运行必要的代码。
You can try to use
blur
event. Of course it also has it's limitations but it's just another suggestion :)You can also try to combine the
blur
,onkeyup
andonmouseup
events to try to catch different situations:blur
when the user makes the selection with keybord arrows and hits<Tab>
,onkeyup
when the user makes the selections with keyboard and stays focused on the slider, andonmouseup
when he uses the mouse. It might be even possible to only combine theonkeyup
andonmouseup
.Still you will have to make a simple check if the value has changed or not and run neccessary code only after a change occured.
onchange 工作得很好,但我需要在滑动时更新该值。
http://jsbin.com/vibuc/1/
onchange works just fine , but I needed to update the value while sliding it.
http://jsbin.com/vibuc/1/
让我们向集合添加一个简单的 ES6 替代方案:
在 JSX 中使用时,它看起来像这样:
Let's add a simple ES6 alternative to the collection:
When used in JSX it would look like this: