javascript - 按箭头键时光标会跳跃

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

最佳答案

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

关于javascript - 按箭头键时光标会跳跃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17186970/

相关文章:

javascript - 追加然后关注子输入查询

javascript - 使用 CSS、JavaScript 或 jQuery 在鼠标悬停时更改字词?

javascript - 去除 JavaScript 列表中重复项的最佳方法是什么?

javascript - FirebaseStorage : Deleting a folder and all its contents using node. js

javascript - 检查元素数量 jQuery

jquery - $ ('iframe' ).css ('visibility' ,'hidden' ) 无法在 Google Chrome 中工作

Javascript/Angular : wait for promise to be fulfilled before going to the next iteration in foreach Loop

jQuery:在悬停元素上添加边框,但不在父元素上添加边框

javascript - 如何将数据库集合中的图像 href 显示为实际图像

javascript - 当我再次搜索时,JavaScript 中的表中出现重复行