jquery 函数将“@”符号转换为 "Q "
jquery function trasforms "@" sign into "Q "
我有一个 keyup 函数,我想用它来检测最后输入的字符是否是“@”。
函数:
$("#message").keyup(function (event) {
var last_one=String.fromCharCode(event.which);
console.log(last_one)
if(last_one == "@"){
console.log("there you go")
}
})
当在文本字段中输入“@”符号时,"last_one" 在控制台上显示为 Q,并且在短暂的延迟后两个框出现在不同的行上。我试图检查方框 if(last_one == "box-sign")
,但没用。
编辑:向下滚动!
您可以使用 JavaScript 中的 .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!');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row d-flex justify-content-center">
<div class="col-6">
<input type="text" class="form-control" id="message" placeholder="enter string" />
</div>
</div>
Codepen 示例 here.
编辑:
如果您想检查字符串中是否存在 @ 符号,则可以使用 .indexOf();
函数并围绕它构建条件。
示例:
$('#message').on("keyup", function() {
var str = $(this).val();
if(str.indexOf("@") > -1) {
alert('There you go!');
}
});
Codepen 示例 here.
片段:
$('#message').on("keyup", function() {
var str = $(this).val();
if(str.indexOf("@") > -1) {
alert('There you go!');
}
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row d-flex justify-content-center">
<div class="col-6">
<input type="text" class="form-control" id="message" placeholder="enter string" />
</div>
</div>
我有一个 keyup 函数,我想用它来检测最后输入的字符是否是“@”。
函数:
$("#message").keyup(function (event) {
var last_one=String.fromCharCode(event.which);
console.log(last_one)
if(last_one == "@"){
console.log("there you go")
}
})
当在文本字段中输入“@”符号时,"last_one" 在控制台上显示为 Q,并且在短暂的延迟后两个框出现在不同的行上。我试图检查方框 if(last_one == "box-sign")
,但没用。
编辑:向下滚动!
您可以使用 JavaScript 中的 .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!');
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<div class="row d-flex justify-content-center">
<div class="col-6">
<input type="text" class="form-control" id="message" placeholder="enter string" />
</div>
</div>
Codepen 示例 here.
编辑:
如果您想检查字符串中是否存在 @ 符号,则可以使用 .indexOf();
函数并围绕它构建条件。
示例:
$('#message').on("keyup", function() {
var str = $(this).val();
if(str.indexOf("@") > -1) {
alert('There you go!');
}
});
Codepen 示例 here.
片段:
$('#message').on("keyup", function() {
var str = $(this).val();
if(str.indexOf("@") > -1) {
alert('There you go!');
}
});
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.min.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="row d-flex justify-content-center">
<div class="col-6">
<input type="text" class="form-control" id="message" placeholder="enter string" />
</div>
</div>