简体   繁体   English

Firefox在input.attr(“ type”,“ number”)上触发更改事件

[英]Firefox firing change event on input.attr(“type”, “number”)

I'm experiencing a problem, only in firefox, when an input is focused, and I change the input type from "text" to "number" , a change event is triggered and the input loses focus. 我遇到一个问题,仅在firefox中,当输入被聚焦时,我将输入类型从"text"更改为"number" ,触发了更改事件,并且输入失去了焦点。

What I want to achieve is to change the input to a number field on focus, allow edit, and change the input toLocaleString() on blur. 我要实现的是将输入更改为焦点上的数字字段,允许进行编辑,并在模糊时将输入更改为LocaleString()。

Here are my codes: 这是我的代码:

 $("input").focusin(function() { $(this).val(toNumber($(this).val())); $(this).attr("type", "number"); }); $("input").focusout(function() { $(this).attr("type", "text"); $(this).val("$ " + toCost($(this).val())); }); // Added during edit to make code testable $('input').on('change', function(){ console.log('changed'); }) function parseNumber(value) { var result = parseFloat(toNumber(value)); if (!isNaN(result)) { return result; } return 0; } function toNumber(str) { return str.replace(/\\s/g, "").replace(/\\$/g, "").replace(/,/g, ""); } function toCost(number) { return parseNumber(number).toLocaleString(); } 
 <!-- Added during edit to make code testable --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <input/> 

I can't figure out why in Firefox the input is triggering a change event and losing focus when I select it. 我不知道为什么在Firefox中选择输入时会触发更改事件并失去焦点。 It works the way I want it to when I try to use $("#myInput").focusin() on the console though. 当我尝试在控制台上使用$("#myInput").focusin()时,它会按照我想要的方式工作。

It works fine on Chrome and Microsoft Edge. 它可以在Chrome和Microsoft Edge上正常运行。

Thanks in advance for taking a look at this! 在此先感谢您看一下!

I'm currently using this as a solution: 我目前正在使用它作为解决方案:

 function is_firefox() { return navigator.userAgent.search("Firefox") >= 0; } $("input").focusin(onFocusIn); $("input").focusout(onFocusOut); $('input').change(onInputChange); function onFocusIn() { $(this).val(toNumber($(this).val())); $(this).attr("type", "number"); // remove on 'change' and 'focusout', and add back after a short delay if (is_firefox()) { $(this).off("change"); $(this).off("focusout"); setTimeout(function(element){ element.change(onInputChange); element.focusout(onFocusOut); }, 15, $(this)); } } function onFocusOut() { $(this).attr("type", "text"); $(this).val("$ " + toCost($(this).val())); } function onInputChange() { console.log("changed"); } $("input").focusout(); function parseNumber(value) { var result = parseFloat(toNumber(value)); if (!isNaN(result)) { return result; } return 0; } function toNumber(str) { return str.replace(/\\s/g, "").replace(/\\$/g, "").replace(/,/g, ""); } function toCost(number) { return parseNumber(number).toLocaleString(); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script> <input value="1000"/> 

I don't think it's the perfect solution, but it works for me now. 我认为这不是完美的解决方案,但现在对我有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 输入类型=文件onchange事件未在google chrome / firefox中触发 - input type=file onchange event is not firing in google chrome/firefox 更改“输入”事件处理程序中的输入值可防止Chrome和IE中的“更改”事件触发(但不会在Firefox中) - Changing input value inside “input” event handler prevents “change” event firing in Chrome and IE (but not in Firefox) 当输入类型为数字时,Firefox 会自动触发 focusout/blur 事件 - Firefox fires focusout / blur event automatically when input type is number Firefox 上的输入类型编号:单击旋转按钮不会触发焦点事件 - input type number on Firefox: click on spin button not fire focus event React输入类型更改触发Firefox中的模糊事件 - React input type change fires blur event in Firefox <input> Firefox中的表排序器“ sortBegin”事件未触发模糊事件 - <input> blur event is not firing on tablesorter “sortBegin” EVENT in firefox 点击事件未触发输入类型=提交 - click event not firing on input type=submit 事件不会在附加元素的输入更改时触发 - Event not firing on input change for appended elements 当输入的值为空格时,输入类型 = 电子邮件的更改事件不会触发 - Input type=email's change event is not firing when the value entered is whitespace <input type="“file”">导航回页面时再次触发更改事件 - <input type=“file”> change event firing again when navigating back to the page
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM