Hy人,
我在我的应用程序中为少数字段使用jquery.maskedit插件,我无法想出构建这样的掩码的方法:
文件:000000-0
我需要始终用零和从右到左的方向填充字段。
是否有任何jQuery插件或有人知道如何使用jquery.maskedit设置此掩码?
答案 0 :(得分:0)
免责声明:有错误。但这是一种方法。
$.fn.specialMask = function () {
this.on('click focus', function () {
var self = this;
setTimeout(function() {self.value = self.value;}, 15); //move cursor to end
})
.on('keypress', function (e) {
var
num,
$this = $(this),
cur = $this.val().match(/Document: ([0-9]*\-[0-9])/); //get value
if (cur[1][0] !== '0') { return; } //input is full
if (e.keyCode >= 48 && e.keyCode <= 57) { //numeric keys
num = e.keyCode - 48;
} else if (e.keyCode >= 96 && e.keyCode <= 105) { //numpad
num = e.keyCode - 96;
} else {
return;
}
cur = cur[1].replace('-', '').substr(1) + '-' + num; //shift value left
$this.val('Document: ' + cur); //update input
});
};
$('#tehInput').mask('Document: 999999-9', {placeholder: '0'}).specialMask(); // initialize the input
我使用了jquery.maskedinput.js,因为我找不到你提到的maskedit插件。如果你有一个链接,尝试将它添加到jsfiddle - 我希望这可能与其他插件类似?
如果没有内置到插件中,从右到左替换占位符是很棘手的(如上所示)。我做过的一个更简单的技巧就是制作一个像这样的虚假输入:
<div class="input-wrapper">
<span class="input-pre"></span>
<input type="text">
</div>
然后,您可以将包装器设置为看起来像普通输入,将实际输入设置为空白,没有边框,并在包装器上发生“单击”事件时聚焦输入。把你的领先0推进到预先跨度,用JS更新它,没有人会更聪明!