我最终修改了内部fn.val
函数,以便如果以编程方式清除元素,它会将值设置为空掩码。
$('#id').val(''); //this will set the value to an empty mask, ie __/__/____
//changes were made to this function
$.fn.val = function ()
{
if (arguments.length == 0)
{
var $self = $(this);
if ($self.data('_inputmask'))
{
if ($self.data('_inputmask')['opts'].autoUnmask)
return $self.inputmask('unmaskedvalue');
else
{
var result = $.inputmask.val.apply($self);
var inputData = $(this).data('_inputmask'), masksets = inputData['masksets'],
activeMasksetIndex = inputData['activeMasksetIndex'];
return result != masksets[activeMasksetIndex]['_buffer'].join('') ? result : '';
}
} else return $.inputmask.val.apply($self);
} else
{
var args = arguments;
//if there are args, and they are undefined, null or empty then use the native .val function
if (args.length > 0 && isNullOrEmpty(args[0]))
{
return $.inputmask.val.apply($(this));
}
else
{
return this.each(function ()
{
var $self = $(this);
var result = $.inputmask.val.apply($self, args);
if ($self.data('_inputmask'))
$self.triggerHandler('setvalue.inputmask');
return result;
});
}
}