javascript - jQuery maskedinput triggers completed twice on paste -
i'm facing bug jquery maskedinput.
pasting value on masked input triggers completed event twice. behavior can seen on oficial website.
open this link , click on demo
. select product key
input , type aa-999-a999
. after that, try paste same value in field.
there other issues related not triggering event on paste, none of them helped me bug...
dows know why or had similar problem?
thanks in advance.
so i've tried simplest case (using this maskedinput.js
, though note edit it, below, , this jquery-1.11.2.js
):
<html> <body> <script type="text/javascript" src="./jquery-1.11.2.js"></script> <script type="text/javascript" src="./jquery.maskedinput.js"></script> <script type="text/javascript"> jquery(function($) { $.mask.definitions['~']='[+-]'; $("#tin").mask("99-9999999",{completed:function(){alert("now typed: "+this.val());}}); }); </script> <table border="0"> <tbody> <tr> <td>tax id</td> <td><input type="text" tabindex="5" id="tin"></td> <td>99-9999999</td> </tr> </tbody> </table> </body> </html>
and edited maskedinput source bit see when , stuff fires. i'll paste.ee here, here main parts...
- find
.on("blur.mask")...
line - note it's reusing same function
blur
,keydown
,input
, ,paste
. - add
console.log
tell specific event firing each time debug pasting.
here's did (note if you're using ie8-, need "protect" console.log
call or run dev tools open):
}).on("blur.mask", blurevent).on("keydown.mask", keydownevent).on("keypress.mask", keypressevent).on("input.mask", function() { console.log("mask called: " + arguments[0].type); // <<< new debugging line. input.prop("readonly") || settimeout(function() { var pos = checkval(!0); input.caret(pos); tryfirecompleted("mask"); }, 0); }), chrome && android && input.off("input.mask").on("input.mask", androidinputevent),
when paste stuff in, 2 event handlers called "same" action.
mask called: paste mask called: input
those both call tryfirecompleted
, fires off .completed
function set in line of our own code, above:
$("#tin").mask("99-9999999",{completed:function(){alert("now typed: "+this.val());}});
if remove paste.mask
event handler, problem go away. here's crucial part:
.on("input.mask paste.mask",
... changes into...
.on("input.mask",
... paste.mask
gone. (and before releasing production, remove console.log
line, of course.)
now might work itself. pastes firing off input
, having paste
might overkill. i'd check around more before used permanent fix. did try right-clicking paste mouse, fine change. not sure when paste
fired , input
isn't.
if there is edge case paste
called , input
isn't (or case want tryfirecompleted
fire twice), you'll need find way suppress second tryfirecompleted
more convoluted code.
without completed
function, though, firing off twice wouldn't obvious problem, don't think, explains why might have slipped through. find.
Comments
Post a Comment