[英]Which event type should I use with inputs of type 'range'?
I have a form with several inputs of type 'range', and which has a submit button that's hidden until each range input fires an event. 我有一个带有多个类型为“范围”的输入的表单,该表单的提交按钮一直隐藏,直到每个范围输入都触发一个事件为止。 I'm curious as to which type of event to select for this purpose. 我很好奇要为此选择哪种类型的事件。
If using 'click' events, some older browsers (ie Windows' Safari) won't always register the events. 如果使用“单击”事件,某些较旧的浏览器(例如Windows的Safari)将不会始终注册事件。 It's probably not a good idea to use 'mouseup', since on legacy browsers, or on some mobile browsers, the range inputs will instead be generated as regular input boxes. 使用“ mouseup”可能不是一个好主意,因为在旧版浏览器或某些移动浏览器上,范围输入将作为常规输入框生成。 Doing something like the following, simply does not trigger an event on the Opera browser running on Android (on which the range inputs are rendered as regular text input boxes): 进行以下操作,就不会触发在Android上运行的Opera浏览器上的事件(在该浏览器上,范围输入显示为常规文本输入框):
$('#my_form input').on("mouseup change", function(){ ... }
So my question is, which event type should I choose that would work well on legacy, mobile, and modern browsers, and would avoid the problems listed above? 所以我的问题是,我应该选择哪种事件类型在旧版,移动和现代浏览器上都可以正常使用,并且可以避免上述问题?
Apparently Chrome and Safari are wrong: onchange should only be triggered when the user releases the mouse. 显然,Chrome和Safari是错误的:仅当用户释放鼠标时才触发onchange。 To get continuous updates, you should use the oninput event, which will capture live updates in Firefox, Safari and Chrome, both from the mouse and the keyboard. 要获取连续更新,您应该使用oninput事件,该事件将从鼠标和键盘捕获Firefox,Safari和Chrome中的实时更新。
However, oninput is not supported in IE10, so your best bet is to combine the two event handlers, like this: 但是,IE10不支持oninput,因此最好的选择是结合两个事件处理程序,如下所示:
<span id="valBox"></span>
<input type="range" min="5" max="10" step="1"
oninput="showVal(this.value)" onchange="showVal(this.value)">
Check out this Bugzilla thread for more information. 查看此Bugzilla线程以获取更多信息。
You can use the change event: 您可以使用change事件:
$('#my_form input').on("change", function(){ ... }
or: 要么:
$('#my_form input').change(function(){ ... }
This will work even if the browser renders the range input as a regular text input. 即使浏览器将范围输入呈现为常规文本输入,这也将起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.