Javascript 按箭头键时,光标正在跳跃
我有一个文本框,其中不能输入禁止字符 但是,当文本框中填充了数据,我将焦点放在文本框的中间,然后我使用箭头键向左和向右移动,然后它跳转到文本框的末尾时,这是可行的Javascript 按箭头键时,光标正在跳跃,javascript,jquery,Javascript,Jquery,我有一个文本框,其中不能输入禁止字符 但是,当文本框中填充了数据,我将焦点放在文本框的中间,然后我使用箭头键向左和向右移动,然后它跳转到文本框的末尾时,这是可行的 如果我在文本框的中间键入一个字符,则它又到了结束/< $('[id$=txtClient]').keyup(function () { EnableClientValidateButton(); // When the textbox changes, the user has the ability to valid
如果我在文本框的中间键入一个字符,则它又到了结束/<
$('[id$=txtClient]').keyup(function () {
EnableClientValidateButton(); // When the textbox changes, the user has the ability to validate the client
ChangeColorClient("0"); // The color is changed to white, to notify the user the client is not validated yet.
var $el = $('[id$=txtClient]'); // the text element to seach for forbidden characters.
var text = $el.val(); // The value of the textbox
text = text.split("#").join("");//remove occurances of forbidden characters, in this case #
$el.val(text);//set it back on the element
});
如果用户按下向右或向左箭头,可以防止执行代码。 为此,您只需添加以下条件:
if(e.which != 37 && e.which != 39){
你可以找到钥匙代码
您的完整代码是:
$('[id$=txtClient]').keyup(function () {
if(e.which != 37 && e.which != 39){
EnableClientValidateButton();
ChangeColorClient("0");
var $el = $('[id$=txtClient]');
var text = $el.val();
text = text.split("#").join("");
$el.val(text);//set it back on the element
}
});
只需防止按下键时的默认操作(keydown不会给出一致的字符码): 这只会阻止
#
,其余部分保持原样
这有点令人不快,我不是100%高兴,但它解决了您遇到的所有问题
$("[id$=txtClient]").keyup(function (e) {
var text = $(this).val();
if (text.indexOf("#") > -1) {
text = text.replace("#", "");
$(this).val(text);
}
});
下面是一个JSFIDLE示例
Javascript允许您为输入设置光标位置 我发现了两个有用的函数:
- getCaretPosition-
- setCaretPosition-
function getCaretPosition (elem) {
// Initialize
var iCaretPos = 0;
// IE Support
if (document.selection) {
// Set focus on the element
elem.focus ();
// To get cursor position, get empty selection range
var oSel = document.selection.createRange ();
// Move selection start to 0 position
oSel.moveStart ('character', -elem.value.length);
// The caret position is selection length
iCaretPos = oSel.text.length;
}
// Firefox support
else if (elem.selectionStart || elem.selectionStart == '0')
iCaretPos = elem.selectionStart;
// Return results
return (iCaretPos);
}
function setCaretPosition(elem, caretPos) {
if(elem != null) {
if(elem.createTextRange) {
var range = elem.createTextRange();
range.move('character', caretPos);
range.select();
}
else {
if(elem.selectionStart) {
elem.focus();
elem.setSelectionRange(caretPos, caretPos);
}
else
elem.focus();
}
}
}
$('[id$=txtClient]').keyup(function () {
EnableClientValidateButton(); // When the textbox changes, the user has the ability to validate the client
ChangeColorClient("0"); // The color is changed to white, to notify the user the client is not validated yet.
var $el = $('[id$=txtClient]'); // the text element to seach for forbidden characters.
var text = $el.val(); // The value of the textbox
text = text.split("#").join("");//remove occurances of forbidden characters, in this case #
var pos = getCaretPosition(this);
$el.val(text);//set it back on the element
setCaretPosition(this, pos);
});
您是否尝试过使用
按键
事件
报告警告了平台之间可能存在的行为差异
至少在Firefox中,e.
对应于转换后键入字符的ascii码:
$('#txtClient').keypress(function (e) {
console.log('keypress:', e.which);
if (e.which == 35) {
return false;
}
});
只需防止按键#不是更容易吗?用你的例子,你没有解决在文本框中间打字的问题。@阿切尔,你说得对。我忘了那部分。。。我再看看。你阻止他们进入的方式是错误的。你应该检查keyup,不要在那里使用#键。我刚给你做了一个演示,但Christoph先到了:)现在可以了,比我的答案好。试过演示吗?哪种浏览器?我已经为你更改了字符代码-应该是222,直到在我的任何浏览器(即9、Chrome 27、Firefox…)中都无法使用@Steve,因为Archer将其更改为错误。不幸的是,一些特殊钥匙在不同的位置有不同的钥匙代码browsers@LuisValencia显然,
#
是一个键码依赖于操作系统/浏览器/字体的字符。关键代码191和163在所有浏览器中为我工作。不幸的是,当试图在中间插入<代码> > <代码>时,游标仍在跳动。我想你不可能很容易解决这个问题,因为幽闭恐惧症建议的答案很好,但并非所有浏览器都支持CaretPosition。谢谢!(注:在Chrome和Opera中也有作品)
$('#txtClient').keypress(function (e) {
console.log('keypress:', e.which);
if (e.which == 35) {
return false;
}
});