按箭头键时光标跳跃

时间:2013-06-19 08:55:20

标签: javascript jquery

我有一个文本框,其中禁止输入禁止的字符。 #。

但是,当文本框中填入数据时,我将焦点放在文本框的中间,然后我使用箭头键左右移动,然后跳转到文本框的末尾

如果我也在文本框的中间键入一个字符,它会再次结束

$('[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
    });

5 个答案:

答案 0 :(得分:3)

Javascript允许您设置输入的光标位置。

我发现了两个有用的功能:

解决方案可能如下所示:

  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);
});

答案 1 :(得分:1)

这有点不愉快,我不是百分之百满意,但它解决了你所有的问题...

$("[id$=txtClient]").keyup(function (e) {
    var text = $(this).val();
    if (text.indexOf("#") > -1) {
        text = text.replace("#", "");
        $(this).val(text);
    }
});

这是一个jsFiddle示例......

http://jsfiddle.net/E4cBK/

答案 2 :(得分:1)

您是否尝试过使用keypress活动?

documentation警告平台之间可能出现的行为差异。

至少在Firefox中,e.which对应于转换后输入字符的ascii代码:

$('#txtClient').keypress(function (e) {
    console.log('keypress:', e.which);
    if (e.which == 35) {
        return false;
    }
});

updated fiddle

答案 3 :(得分:0)

如果用户按右箭头或左箭头,您可以阻止执行代码。 要做到这一点,你只需要添加这个条件:

if(e.which != 37 && e.which != 39){  

您可以找到关键代码here

您的完整代码将是:

$('[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
    }
});

LIVING EXAMPLE

答案 4 :(得分:0)

只是阻止 keypress 上的默认操作(keydown不提供一致的charCodes):

$('[id$=txtClient]').keypress(function (e) {
        if (String.fromCharCode(e.which) == '#'){
            e.preventDefault();
        }
});

这只会阻止#并保留其余部分。

Here you go;)