简体   繁体   English

我应将哪种事件类型与“范围”类型的输入一起使用?

[英]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.

相关问题 这种类型的下拉菜单应该使用什么jquery事件 - what jquery event should i use for this type of dropdown menu 我应该使用哪个HTML事件监听器? - Which HTML event listener should I use? 如果我需要将变量传递给服务器但查询是 SELECT,我应该使用哪种类型的 http 请求? - Which type of http request should I use if I need pass a variable to server but the query is a SELECT? 对于结合了文本和数字的代码,我应该使用什么输入类型? 如何在 TypeScript 中声明以避免错误? - What input type should I use for code which combines text and numbers? How to declare in TypeScript to avoid errors? HTML keyup / keydown 有很多检测键类型的方法。 我应该使用哪个? - HTML keyup / keydown has a lot of ways to detect key type. Which should I use? 依赖输入类型范围的正确算法 - Right algorithm for dependent inputs type range 我应该使用什么类型的加密 - What type of encryption should i use 我应该在Javascript类型中使用typeof相等吗? - Should I use typeof in Javascript type equality? 哪个事件可用于从输入中发出更改? - Which event to use for emitting change from inputs? 我应该如何在 Typescript 中定义一个动态类型,它应该保留用户输入的实际类型 - How should I define a dynamic type in Typescript which should preserve the actual type entered by user
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM