首页 > 解决方案 > 提交表单后如何让输入值显示在屏幕上?

问题描述

我正在制作一个应用程序来检查一个单词是否是回文,如果是回文,它会在提交表单后在屏幕上显示该单词并说(单词+“是回文!”)。我什至无法将它发送到 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);

标签: javascripthtmlformsalgorithmpalindrome

解决方案


只是在构建反向单词时连接

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>


推荐阅读