gpt4 book ai didi

Javascript 和 JSP 未重新加载

转载 作者:行者123 更新时间:2023-11-28 09:37:54 27 4
gpt4 key购买 nike

嘿,大家正在我们的 jsp 页面上进行一些表单验证。

当您在容量字段中未输入任何值或输入非数字值时,我会出现警告框。但单击“确定”后,页面会重定向到错误:

HTTP Status 500 - For input string: ""

或者在容量字段中输入“a”时

HTTP Status 500 - For input string: "a"

如何重新加载页面而不是将我带到错误页面?

我已经尝试了许多内置的 Javascript 函数,但仍然定向到错误页面,例如 location.reload() 等。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Mars University Lab System</title>
<link rel="stylesheet" href="style.css" type="text/css" media="screen">
</head>

<body>
<jsp:include page="headerLecturer.jsp"/>
<script>

function chkNumeric(strString)
// check for valid numeric strings
{
var strValidChars = "0123456789.-";
var strChar;
var blnResult = true;

if (strString.length == 0) return false;

// test strString consists of valid characters listed above
for (i = 0; i < strString.length && blnResult == true; i++)
{
strChar = strString.charAt(i);
if (strValidChars.indexOf(strChar) == -1)
{
blnResult = false;
}
}
return blnResult;
}


function validateForm()
{
var capacity=document.forms["createLabs"]["capacity"].value;
var day=document.forms["createLabs"]["day"].value;
var time=document.forms["createLabs"]["time"].value;
var room=document.forms["createLabs"]["room"].value;

if (capacity==null || capacity=="")
{
alert("Capacity must be filled out");
document.createLabs.capacity.focus();
return false;
}
else if(day==null || day=="")
{
alert("Day must be filled out");
document.createLabs.day.focus();
return false;
}
else if(time==null || time=="")
{
alert("Time must be filled out");
document.createLabs.time.focus();
return false;
}
else if(room==null || room=="")
{
alert("Room must be filled out");
document.createLabs.room.focus();
return false;

}
else if (chkNumeric(document.createLabs.capacity.value) == false)
{
alert("Please check - non numeric value!");
document.createLabs.room.focus();
return false;

}
else if (chkNumeric(document.createLabs.room.value) == false)
{
alert("Please check - non numeric value!");
document.createLabs.room.focus();
return false;
}




var link2 = document.getElementById('submit');

link2.onclick = function ()
{
if( confirm("Please fill in all the required fields") )
{
window.location.reload();
}
return false;
}


}


</script>

<tr>
<td>
</td>
</tr>

<tr>
<td>
<div id = "centrecontent">
<br>
<h3>Create Labs</h3>
<%String[] list1 = (String[])request.getAttribute("res1"); %>
<%String[] list2 = (String[])request.getAttribute("res2"); %>


<form name ="createLabs" ACTION="CreateLab" method="post">

Subject: <select name="subject_id">
<%
for(int i=0; i<list1.length; i++)
{
//for(int m=0; m<list2.length;i++)
//{
out.println("<option value="+list1[i]+"> "+list2[i]+" </option>");

//}
out.println("list1:"+list1[0]);
}
%>
</select><br><br>
Capacity: <input type="text" name="capacity"/><br /><br>
<input type="hidden" name="spots_left"/>
Day: <input type="text" name="day"/><br /><br>
Time (HH:MM:SS): <input type="time" name="time"/><br /><br>
Room: <input type="text" name="room"/><br /><br>
<input type=SUBMIT value="Submit" name="Submit" onClick="validateForm()" />

</form>
</div>

<jsp:include page="footer.jsp"/>


</body>

</html>

最佳答案

问题来自服务器端,因此问题出在验证上在 JavaScript 函数中

我更改了validateForm,但没有检查。

也许会有帮助

      function validateForm()
{
var capacity=document.forms["createLabs"]["capacity"].value;
var day=document.forms["createLabs"]["day"].value;
var time=document.forms["createLabs"]["time"].value;
var room=document.forms["createLabs"]["room"].value;

if (capacity === undefined)
{
alert("Capacity must be filled out");
document.createLabs.capacity.focus();
return false;
}

if(day === undefined)
{
alert("Day must be filled out");
document.createLabs.day.focus();
return false;
}

if(time === undefined)

{
alert("Time must be filled out");
document.createLabs.time.focus();
return false;
}

if(room === undefined)

{
alert("Room must be filled out");
document.createLabs.room.focus();
return false;

}

if ( isNaN(capacity))
{
alert("Please check - non numeric value!");
document.createLabs.room.focus();
return false;
}

if ( isNaN(room))
{
alert("Please check - non numeric value!");
document.createLabs.room.focus();
return false;
}

if(room === undefined)
{
alert("Please check - non numeric value!");
document.createLabs.room.focus();
return false;
}




var link2 = document.getElementById('submit');

link2.onclick = function ()
{
if( confirm("Please fill in all the required fields") )
{
window.location.reload();
}
return false;
}


}

此外,您需要为“onsubmit”事件放置 validateForm 触发器

<form name ="createLabs" ACTION="CreateLab" method="post" onsubmit="return validateForm()">

Subject: <select name="subject_id">
<%
for(int i=0; i<list1.length; i++)
{
//for(int m=0; m<list2.length;i++)
//{
out.println("<option value="+list1[i]+"> "+list2[i]+" </option>");

//}
out.println("list1:"+list1[0]);
}
%>
</select><br><br>
Capacity: <input type="text" name="capacity"/><br /><br>
<input type="hidden" name="spots_left"/>
Day: <input type="text" name="day"/><br /><br>
Time (HH:MM:SS): <input type="time" name="time"/><br /><br>
Room: <input type="text" name="room"/><br /><br>
<input type="submit" value="Submit" name="Submit" />

</form>

关于Javascript 和 JSP 未重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12777590/

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