Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jquery函数trasforms"@&引用;签入";Q";_Javascript_Jquery_Keyup - Fatal编程技术网

Javascript jquery函数trasforms"@&引用;签入";Q";

Javascript jquery函数trasforms"@&引用;签入";Q";,javascript,jquery,keyup,Javascript,Jquery,Keyup,我有一个键控功能,我想用它来检测最后输入的字符是否为“@” 功能: $("#message").keyup(function (event) { var last_one=String.fromCharCode(event.which); console.log(last_one) if(last_one == "@"){ console.log("there you go")

我有一个键控功能,我想用它来检测最后输入的字符是否为“@”

功能:

 $("#message").keyup(function (event) {
            var last_one=String.fromCharCode(event.which); 
            console.log(last_one)
            if(last_one == "@"){
                console.log("there you go")
            }
        })

当在文本字段中输入“@”符号时,“最后一个”在控制台上显示为Q,两个框在短时间延迟后分别显示在不同的行上。我试图检查框
if(last_one==“box sign”)
,但没有成功。

编辑:向下滚动

您可以使用
.slice()函数获取字符串的最后一个字符

示例:

$('#message').on("keyup", function() {
    var str = $(this).val();
    var lastChar = str.slice(-1);

    if(lastChar == "@") {
      alert('There you go!');
    }
});
.slice()函数基本上取字符串的长度,在本例中,在索引中减去1,然后成为字符串的最后一个字符。那么你所要做的就是让你的生活有条件

片段:

$('#message').on("keyup", function() {
    var str = $(this).val();
    var lastChar = str.slice(-1);

    if(lastChar == "@") {
      alert('There you go!');
    }
});
$('#message')。在(“keyup”,function()上{
var str=$(this.val();
var lastChar=str.slice(-1);
如果(lastChar==“@”){
警惕(‘你来了!’);
}
});

编辑:向下滚动

您可以使用
.slice()函数获取字符串的最后一个字符

示例:

$('#message').on("keyup", function() {
    var str = $(this).val();
    var lastChar = str.slice(-1);

    if(lastChar == "@") {
      alert('There you go!');
    }
});
.slice()函数基本上取字符串的长度,在本例中,在索引中减去1,然后成为字符串的最后一个字符。那么你所要做的就是让你的生活有条件

片段:

$('#message').on("keyup", function() {
    var str = $(this).val();
    var lastChar = str.slice(-1);

    if(lastChar == "@") {
      alert('There you go!');
    }
});
$('#message')。在(“keyup”,function()上{
var str=$(this.val();
var lastChar=str.slice(-1);
如果(lastChar==“@”){
警惕(‘你来了!’);
}
});


使用event.key而不是String.fromCharCode(event.which);在我的测试中,我从来没有看到过q。您在哪个平台和浏览器上测试?非常感谢,但这会在控制台上返回@1:367 Control 1:367 AltGraph,并且仍然不会触发函数的if部分。您在哪个平台和浏览器上测试。它适用于我在chrome.windows/chrome-opera-edge上的ubuntu 20.04,适用于keydown btw.use event.key而不是String.fromCharCode(event.which);在我的测试中,我从来没有看到过q。您在哪个平台和浏览器上测试?非常感谢,但这会在控制台上返回@1:367 Control 1:367 AltGraph,并且仍然不会触发函数的if部分。您在哪个平台和浏览器上测试。我在ubuntu 20.04的chrome.windows/chrome-opera-edge上使用它,顺便说一句,它适用于keydown。非常感谢@Martin,但它仅在字符位于字符串末尾时才有效。是否要检查字符是否位于字符串中的任何索引处?是的I$('#message')。on(“keyup”,function()){var lastChar=event.key if(lastChar==“@”){console.log('There you go!');});这个很好用。非常感谢您的帮助,非常感谢。如果您想知道@符号是否出现在字符串中的任何位置,请检查编辑后的答案。:)您是最棒的!非常感谢@Martin,但它只在字符位于字符串末尾时才起作用。您想检查字符是否位于字符串中的任何索引处吗?是的,我想$(“#message”).on(“keyup”,function(){var lastChar=event.key if(lastChar=“@”){console.log('There you go!');});这一个很好用。非常感谢您的帮助,非常感谢。如果您想知道@符号是否出现在字符串中的任何位置,请检查编辑后的答案。:)你是最棒的!