当将按键输入到jQuery Masked Input中时,如何防止现有值向右移动? [英] How do I keep existing values from shifting to the right when entering keypresses into a jQuery Masked Input?
问题描述
我有一个文本框正在用作计时器显示"hh:mm:ss"当我选择该框并按一个数字时,它将在光标位置插入数字,但没有替换该位置的值位置,它将所有现有值移开.例如,计时器文本框显示为"01:00:35",而我将第一分钟位置替换为1,则计时器文本框将显示为"01:10:03".
I have a text box I'm using as a timer display "hh:mm:ss" When I select the box and press a number, it inserts the number at the cursor location, but instead of replacing the value at that position, it shifts all existing values over. For example, the timer text box reads "01:00:35" and I replace the first minute position with 1, the timer text box will then read "01:10:03."
有人知道如何强制文本框在光标位置替换而不是插入吗?
Anybody know how to force the text box to replace, instead of insert, at the cursor position?
我尝试拦截onKeyPress,手动进行替换,重写整个计时器文本,然后返回false.但是,这不适用于jQuery屏蔽输入,因为我的函数先运行.
I've tried intercepting onKeyPress, doing the replace manually, rewriting the entire timer text, and then returning false. But, that doesn't worked with a jQuery masked input, because my function runs first.
推荐答案
看起来这是被屏蔽的输入插件的功能.您可以尝试通过替换以下方法来修改插件,然后在调用它时将noshift选项设置为true(警告我对此进行了最少的测试,但似乎可行)- $('#showTime').mask("99:99:99",{noshift:true});
Looks like this is a feature of the masked input plugin. You can try modifying the plugin by replacing the following methods and then set the noshift option to true when you call the it(warning I did minimal testing on this, but it seemed to work) - $('#showTime').mask("99:99:99",{noshift:true});
function shiftL(pos) {
if(!settings.noshift){
while (!tests[pos] && --pos >= 0);
for (var i = pos; i < len; i++) {
if (tests[i]) {
buffer[i] = settings.placeholder;
var j = seekNext(i);
if (j < len && tests[i].test(buffer[j])) {
buffer[i] = buffer[j];
} else
break;
}
}
}
writeBuffer();
input.caret(Math.max(firstNonMaskPos, pos));
};
function keypressEvent(e) {
if (ignore) {
ignore = false;
//Fixes Mac FF bug on backspace
return (e.keyCode == 8) ? false : null;
}
e = e || window.event;
var k = e.charCode || e.keyCode || e.which;
var pos = $(this).caret();
if (e.ctrlKey || e.altKey || e.metaKey) {//Ignore
return true;
} else if ((k >= 32 && k <= 125) || k > 186) {//typeable characters
var p = seekNext(pos.begin - 1);
if (p < len) {
var c = String.fromCharCode(k);
if (tests[p].test(c)) {
if(!settings.noshift) shiftR(p);
buffer[p] = c;
writeBuffer();
var next = seekNext(p);
$(this).caret(next);
if (settings.completed && next == len)
settings.completed.call(input);
}
}
}
return false;
};
这篇关于当将按键输入到jQuery Masked Input中时,如何防止现有值向右移动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!