【问题标题】:JavaScript is not working with JSP page in ECLIPSE IDEJavaScript 不适用于 ECLIPSE IDE 中的 JSP 页面
【发布时间】:2020-10-11 21:16:51
【问题描述】:

enter image description here

这是学生注册表格,它收集信息并将信息发送到“操作”中提到的 Servlet“StudentRegisterSrv”。但是用于密码确认匹配的 JS 代码不起作用。我已经尝试过内部(在同一个文件上)和外部 JS(通过链接)。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Register | Home</title>
<script type="text/javascript">
function checkPassword(form) { 
                password1 = form.password.value; 
                password2 = form.password2.value; 
                // If password not entered 
                if (password == '') 
                    alert ("Please enter Password"); 
                // If confirm password not entered 
                else if (password2 == '') 
                    alert ("Please enter confirm password");                       
                // If Not same return False.     
                else if (password != password2) { 
                    alert ("\nPassword did not match: Please try again...") 
                    return false; 
                }  
                // If same return True. 
                else{ 
                    alert("Password Match: Welcome to GeeksforGeeks!") 
                    return true; 
                } 
            } 
</script>            
<link rel="stylesheet" href="css/body.css">
</head>
<body>
    <h1>Student Registration</h1>
    <form action="StudentRegisterSrv" method="post">
        <input type="text" name="name" placeholder="Name" required>  <br><br> 
        <input type="email" name="email" placeholder="Email" required>  <br> <br> 
        <input type="number" name="phone" placeholder="Mobile No." required>    <br><br> 
        <input id="pwd1" type="password" name="password" placeholder="Password" required>   <br><br>    
        <input id="pwd2" type="password" name="password2" placeholder="Password Again" required>    <br><br>
        <input type="submit" value="Register" onSubmit = "return checkPassword(this)"> 
        <input type="reset" value=" RESET">
    </form>
</body>
</html>

【问题讨论】:

    标签: javascript html eclipse jsp


    【解决方案1】:

    您需要将onSubmit="return checkPassword()" 添加到您的表单标签中,以便在单击提交按钮时调用它,如果false 将从js 返回,则表单将不会提交,否则如果true 表单将获得提交。还我已经使用document.getElementById("pwd1").value 来获取密码字段的值和固定的拼写错误。

    演示代码

    function checkPassword() {
      //get value by id
      var password1 = document.getElementById("pwd1").value;
      var password2 = document.getElementById("pwd2").value;
      // If password not entered 
      if (password1 == '')
        alert("Please enter Password");
      // If confirm password not entered 
      else if (password2 == '')
        alert("Please enter confirm password");
      // If Not same return False.     
      else if (password1 != password2) {
        alert("\nPassword did not match: Please try again...")
        return false; //will return false and prevent form to submit
      }
      // If same return True. 
      else {
        alert("Password Match: Welcome to GeeksforGeeks!")
        return true; //will return true and submit the form
      }
    }
    <h1>Student Registration</h1>
    <form name="form" action="StudentRegisterSrv" method="post" onSubmit="return checkPassword()">
      <input type="text" name="name" placeholder="Name" required> <br><br>
      <input type="email" name="email" placeholder="Email" required> <br> <br>
      <input type="number" name="phone" placeholder="Mobile No." required> <br><br>
      <input id="pwd1" type="password" name="password" placeholder="Password" required> <br><br>
      <input id="pwd2" type="password" name="password2" placeholder="Password Again" required> <br><br>
      <input type="submit" value="Register">
      <input type="reset" value=" RESET">
    </form>

    【讨论】:

    • 非常感谢您的回答。你说的我试过了,还是不行。
    • 在 Eclipse IDE 中,在编写 JavaScript 时,代码完成(ctrl + 空格)也不起作用。这是否与上述问题有关。
    • 你试过运行它吗?它在你那里工作吗?
    • 是的,您可以运行上面的代码 sn-p 并检查一次。还要检查您的浏览器控制台是否显示任何错误?我在 netbeans 中试过这个。
    • 好的,我试试看控制台。
    猜你喜欢
    • 2011-08-17
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 2011-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多