javascript - 提交表单后如何让输入值显示在屏幕上?
问题描述
我正在制作一个应用程序来检查一个单词是否是回文,如果是回文,它会在提交表单后在屏幕上显示该单词并说(单词+“是回文!”)。我什至无法将它发送到 console.log 或者不知道如何让它识别已输入的值。
html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pallindrome Checker</title>
</head>
<body>
<h1>Pallindrome Checker</h1>
<form id='form'>
<input id = 'pal' type = 'text'/>
<input id = 'submit' type = 'button' value='submit' />
</form>
<script src='script.js'></script>
</body>
</html>
Javascript代码
function pallindrome(e){
e.preventDefault();
let letters = [];
let rword="";
let word = document.getElementById("pal").value = "";
for (let i = 0; i < word.length; i++){e
letters.push(word[i]);
}
for (let i = 0; i < word.length; i++){
rword += letters.pop();
}
if(rword === word) {
console.log(word + "is a pallindrome!")
}
else{
console.log(word + "is not a pallindrome!")
}
}
document.getElementById("submit").addEventListener("click", pallindrome, false);
解决方案
只是在构建反向单词时连接
function pallindrome(e){
e.preventDefault();
let letters = [];
let rword='';
let word = document.getElementById("pal").value ;
for (let i = 0; i < word.length; i++){e
rword =rword + word[word.length-1-i];
}
if(rword === word) {
console.log(word + " is a pallindrome!")
}
else{
console.log(word + " is not a pallindrome!")
}
}
document.getElementById("submit").addEventListener("click", pallindrome);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pallindrome Checker</title>
</head>
<body>
<h1>Pallindrome Checker</h1>
<form id='form'>
<input id = 'pal' type = 'text'/>
<input id = 'submit' type = 'button' value='submit' />
</form>
<script src='script.js'></script>
</body>
</html>
推荐阅读
- typescript - 为什么 Nx 缓存不跟踪我的工作区的某些更改?
- java - Docker CMD - 脚本路径
- reactjs - 在异步调用期间,解构赋值似乎会影响在它之前发生的变量
- sql - PostgreSQL,SQL:如何在现成的函数中使用行值作为参数?
- data-structures - 删除节点后增加展开树高度的原因可能是什么?
- python - 生成相关随机数矩阵
- json - Laravel 5.8 PhpUnit 测试控制器失败
- html - 与不同情况下的其他单位相比,百分比如何真正起作用
- tcl - 如何在 Tcl 中向字典添加附加键
- java - 当我尝试为 Selenium Web 驱动程序创建 GUI 时出现“应用程序启动方法 java.lang.reflect.InvocationTargetException 中的异常”