gpt4 book ai didi

JavaScript 按键仅在文本字段包含 ';' 时起作用

转载 作者:行者123 更新时间:2023-11-27 22:51:11 25 4
gpt4 key购买 nike

我希望当我输入文本并按 Enter 键时文本框消失。但由于某种原因,只有当我输入“;”时它才会消失。任何其他字符似乎都不起作用。

function enterPressed(e, field) {
if (e.keyCode == 13) {
var tb = document.getElementById("searchbox");
eval(tb.value);
if (field.value.length > 0) {
document.getElementById('searchdisappear').style.display='none';
}
}
}
<!DOCTYPE html>

<html lang = "en">

<head>
<link rel="stylesheet" type="text/css" href="./css/main.css">
<script src ="./js/search.js"></script>
</head>

<body>

<!-- SEARCH BAR AND BUTTON -->
<div id="searchdisappear" style="display:block;">
<input id="searchbox" class="search" maxlength="200" type="text" onkeypress="enterPressed(event, searchbox)" placeholder="Search.."/>
<p class="searchtext" onclick="buttonClicked(searchbox)"

</div>
</html>

最佳答案

只需删除不必要的eval函数即可。

function enterPressed(e, field) {
if (e.keyCode == 13) {
var tb = document.getElementById("searchbox");
if (field.value.length > 0) {
document.getElementById('searchdisappear').style.display='none';
}
}
}
<!DOCTYPE html>

<html lang = "en">

<head>
<link rel="stylesheet" type="text/css" href="./css/main.css">
<script src ="./js/search.js"></script>
</head>

<body>

<!-- SEARCH BAR AND BUTTON -->
<div id="searchdisappear" style="display:block;">
<input id="searchbox" class="search" maxlength="200" type="text" onkeypress="enterPressed(event, searchbox)" placeholder="Search.."/>
<p class="searchtext" onclick="buttonClicked(searchbox)"

</div>
</html>

关于JavaScript 按键仅在文本字段包含 ';' 时起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38044681/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com