带表单的 JSP 和 JavaScript

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9461948/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 06:44:03  来源:igfitidea点击:

JSP and JavaScript with form

javascriptformsjsp

提问by Kristal

I have a form in a Registeration.jsp page:

我在 Registeration.jsp 页面中有一个表单:

form name="users" method="post" onSubmit="return checkCorrect()" action="Registeration.jsp">

form name="users" method="post" onSubmit="return checkCorrect()" action="Registeration.jsp">

The Script checkCorrect is written at the start of the page, returns true or false based on information submitted to the form (using getElementById if it matters) and the script definitely works (worked on html page without jsp before)

脚本 checkCorrect 写在页面的开头,根据提交给表单的信息返回真或假(如果重要,使用 getElementById)并且脚本肯定有效(之前在没有 jsp 的 html 页面上工作)

after the form I have this:

在表格之后我有这个:

<% if(request.getMethod().equals("POST")){...}

<% if(request.getMethod().equals("POST")){...}

And I need that the jsp part will work ONLY if and after the form is successfully submitted, but somehow the javascript script is not called and don't work and the form is always submitted.

而且我需要jsp部分仅在表单成功提交时和之后才能工作,但不知何故javascript脚本没有被调用并且不起作用并且表单总是被提交。

What am I doing wrong?

我究竟做错了什么?

edits:
there's no redirection in my page, the javascript check function, the form, and the JSP part that procces the form after submitting it are at the same page.
the jsp part is used to send the data from the form to a database.
the function:

编辑:
我的页面中没有重定向,javascript检查功能,表单和提交表单后处理表单的JSP部分都在同一页面上。
jsp 部分用于将数据从表单发送到数据库。
功能:

function checkCorrect(){
var fullname=document.getElementById("fullname").value;
...
if (response.length==0)
{
return true;
}
else
{
alert ("These problems are found in your form:\n\n"+response);
return false;
}
}

function checkCorrect(){
var fullname=document.getElementById("fullname").value;
...
if (response.length==0)
{
return true;
}
else
{
alert ("这些问题在你的表单中发现:\n\n"+response);
返回假;
}
}

then come the bodyand the form and then
the jsp part:

然后是body表单和
jsp部分:

<%

if(request.getMethod().equals("POST")){

String fullname=request.getParameter("fullname");
and go on.. } % >

<%

if(request.getMethod().equals("POST")){

String fullname=request.getParameter("fullname");
然后继续.. } % >

Solution:
check the JavaScript part really good people it doesn't have compiler so even a tiny problem there can screw up the entire project.

解决方案:
检查 JavaScript 部分非常好的人,它没有编译器所以即使是一个小问题也可能搞砸整个项目。

回答by Sarin J S

<% uname=""+request.getAttribute("username"); %>

This variable will get a value only when you load your page or refresh it.

仅当您加载页面或刷新页面时,此变量才会获得一个值。

I guess your page flow is as follows.

我猜你的页面流程如下。

You have your first page with form, and onsubmit form will call javascript function as

您拥有带有表单的第一页,并且 onsubmit 表单将调用 javascript 函数作为

<form name="users" method="post" onSubmit="returncheckCorrect()" action="Registeration.jsp">

then your javascript will check your answer like :

那么您的 javascript 将检查您的答案,如:

<script type="text/javascript">
function returncheckCorrect() {
    var x = document.getElementsByName("userName")
    if (your condition) {
        alert("Redirecting");
        return true;
    } else {
        alert("Not Redirecting");
        return false;
    }
}
// return false; // lol, see why indentation is useful ?
</script>

then (if (your condition == true)) your java script will redirect to a second page where you want to get the value in a scriptlet like

然后(如果(您的条件 == 真))您的 Java 脚本将重定向到第二个页面,您希望在其中获取脚本中的值,例如

<% uname=""+request.getAttribute("username"); %>

Make sure your code is in this manner.

确保您的代码采用这种方式。

Following is the code which I tried as you said, its working

以下是我按照你所说的尝试的代码,它的工作原理

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
    function chkme() {
        var x = document.getElementById('textfield');
        if (x.value == 'sarin') {
            alert("success");
        } else {
            alert("failed");
        }
        return true;
    }
</script>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <form id="form1" name="form1" method="post" onsubmit="chkme();"
        action="">
        <input type="text" name="textfield" id="textfield" /> <input
            type="text" name="textfield2" id="textfield2" /> <input
            type="submit" name="button" id="button" value="Submit" />
    </form>
    <%
        if (request.getMethod().equals("POST")) {
            String textfield = request.getParameter("textfield");
            String textfield2 = request.getParameter("textfield2");
    %>
    <%=textfield%>
    <br />
    <%=textfield2%>
    <%
        }
    %>
</body>
</html>