首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >替换焦炭OnKeyPress

替换焦炭OnKeyPress
EN

Stack Overflow用户
提问于 2015-05-07 10:15:23
回答 5查看 13.4K关注 0票数 11

我有一个文本区域输入元素,

如果用户键入"@“,我想用@someTextHere替换它。

我使用的是JQuery的keypress事件,但我无法得到我想要的,我一直在字符串的末尾得到"@“,即,”someTextHere“。

有可能吗?

我的守则:

代码语言:javascript
复制
<html>
    <head>
        <script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
    </head>

    <body>
        <textarea id="post-txt"></textarea>
    </body>
</html>

<script>
    $('#post-txt').keypress(function(event){
        var str = $('#post-txt').val(); 

        if(String.fromCharCode(event.which) == '@'){
            $('#post-txt').val(str.substring(0,str.length) + '[TEXT]'); 
            }
    })  
</script>

如果提供援助,将不胜感激。

EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2015-05-07 10:21:31

这是因为他在执行function.you之后添加了字符,可以防止添加字符并将其添加到代码中。

代码语言:javascript
复制
 if(String.fromCharCode(event.which) == '@'){
    event.preventDefault()
    $('#post-txt').val(str + '@[TEXT]'); 
 }
票数 5
EN

Stack Overflow用户

发布于 2018-06-08 14:47:33

下面是可控硅退化的一个很好的解决方案,它考虑到了选择和光标位置。

代码语言:javascript
复制
var replacedChar = '@';
var replacement = '@[SomeTextHere]'
var moveCursorBy = replacement.length - replacedChar.length; //Or 0 if you want the cursor to be after between '@' and '[SomeTextHere]'

$('textarea').keypress(function(e){
  if(e.key == replacedChar){
    // IE
    if(document.selection){
      // Determines the selected text. If no text selected, the location of the cursor in the text is returned
      var range = document.selection.createRange();
      // Place the replacement on the location of the selection, and remove the data in the selection
      range.text = replacement;
      // Chrome + FF
    } else if(this.selectionStart || this.selectionStart == '0') {
      // Determines the start and end of the selection.
      // If no text selected, they are the same and the location of the cursor in the text is returned
      // Don't make it a jQuery obj, because selectionStart and selectionEnd isn't known.
      var start = this.selectionStart;
      var end = this.selectionEnd;
      // Place the replacement on the location of the selection, and remove the data in the selection
      $(this).val($(this).val().substring(0, start) + replacement +                       $(this).val().substring(end, $(this).val().length));
      // Set the cursor back at the correct location in the text
      this.selectionStart = start + moveCursorBy + 1;
      this.selectionEnd = start + moveCursorBy + 1;
    } else {
      // if no selection could be determined,
      // place the replacement at the end.
      $(this).val($(this).val() + replacement);
    }
    return false;
  }
})
代码语言:javascript
复制
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<textarea></textarea>

票数 2
EN

Stack Overflow用户

发布于 2018-11-16 12:38:39

我冒昧地利用Alexandru Severin发布的函数编写了jquery函数:

代码语言:javascript
复制
$.fn.replaceCharOnKeyPress = function(chr, replacement) {
    var moveCursorBy = replacement.length - chr.length;
    this.each(function() {
        $(this).keypress(function(e) {
            if (e.key == chr) {
                // IE
                if(document.selection) {
                    // Determines the selected text. If no text selected, the location of the cursor in the text is returned
                    var range = document.selection.createRange();
                    // Place the replacement on the location of the selection, and remove the data in the selection
                    range.text = replacement;
                }
                // Chrome + FF
                else if(this.selectionStart || this.selectionStart == '0') {
                    // Determines the start and end of the selection.
                    // If no text selected, they are the same and the location of the cursor in the text is returned
                    // Don't make it a jQuery obj, because selectionStart and selectionEnd isn't known.
                    var start = this.selectionStart;
                    var end = this.selectionEnd;
                    // Place the replacement on the location of the selection, and remove the data in the selection
                    $(this).val($(this).val().substring(0, start) + replacement + $(this).val().substring(end, $(this).val().length));
                    // Set the cursor back at the correct location in the text
                    this.selectionStart = start + moveCursorBy + 1;
                    this.selectionEnd = start + moveCursorBy + 1;
                }
                else {
                    // if no selection could be determined,
                    // place the replacement at the end.
                    $(this).val($(this).val() + replacement);
                }
                return false;
            }
        });
    });
    return this;
};

用法示例:

代码语言:javascript
复制
$(form).find('input.price').replaceCharOnKeyPress(',', '.');
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30098133

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档