我的确认有点问题。
在我的代码中包含一个确认框之前发生的事情是,当我点击一个按钮(name = preQuestion)时,如果有任何验证错误,那么它将在页面上显示验证错误而不提交表单,如果有没有验证错误,那么它将提交表格。
但我现在想要做的事情是哪些我无法工作以及哪些不断给我错误,如果有验证错误那么它会做它当前正在做的事情,但如果没有验证错误,那么我想要一个确认框出现说明......用户将点击确定或取消。如果用户单击确定然后提交表单,如果用户单击取消,则关闭确认框并保持在同一页面上。我哪里错了?
下面是javascript代码(我缩短了代码,因此它不包含所有验证代码,或者代码溢出):
function validation() {
var isDataValid = true;
var sessionNoO = document.getElementById("sessionNo");
var questionNumberO = document.getElementById("txtQuestion");
var roomTextO = document.getElementById("room");
var errSessionMsgO = document.getElementById("sessionNoAlert");
var errQuesMsgO = document.getElementById("numberAlert");
var errRoomMsgO = document.getElementById("roomAlert");
var trimmedRoomText = roomTextO.value.replace(/^\s+/, '').replace(/\s+$/, '');
if (sessionNoO.value == ""){
errSessionMsgO.innerHTML = "Please Enter in the Number of Sessions you Require";
isDataValid = false;
}else if (sessionNoO.value == 0){
errSessionMsgO.innerHTML = "Number of Sessions Must be More than 0";
isDataValid = false;
}else{
errSessionMsgO.innerHTML = "";
}
if(questionNumberO.value == 0){
errQuesMsgO.innerHTML = "Please Set the Number of Questions";
isDataValid = false;
} else {
errQuesMsgO.innerHTML = "";
}
if (roomTextO.value == ""){
errRoomMsgO.innerHTML = "Please Enter in a Room Number";
isDataValid = false;
}else if (!trimmedRoomText.length){
errRoomMsgO.innerHTML = "Please Enter in a Room Number";
isDataValid = false;
}else{
errRoomMsgO.innerHTML = "";
}
return isDataValid;
}
function submitform()
{
var sessionFormO = document.getElementById("sessionForm");
sessionFormO.submit();
}
document.getElementsByName("prequestion")[0].addEventListener('click', myClickHandler);
function myClickHandler(){
if(validation()){
function showConfirm(){
var confirmMsg=confirm("Make sure that your details are correct, once you proceed after this stage you would
not be able to go back and change any details towards your Session.Are you sure you want to Proceed?");
if (confirmMsg==true)
{
submitform();
}else{
parent.close();
}
}
}
}
Html表单代码(不显示整个表单,以便没有代码溢出):
<form action="QandATable.php" method="post" id="sessionForm">
<p><strong>2: Number of Sessions you Require:</strong> <input type="text" id="sessionNo" onkeypress="return isNumberKey(event)"><br/><span id="sessionNoAlert"></span></p>
<p><strong>8: Room:</strong> <input type="text" id="room"><br/><span id="roomAlert"></span></p> <!-- Enter Room here-->
<table>
<tr>
<th>9: Number of Questions:</th>
<td class="spinner"><input type="text" class="spinnerQuestion" id="txtQuestion" name="textQuestion"></td>
<td><button class="scrollBtn" id="btnQuestionUp" type="button"><img src="Images/black_uppointing_triangle.png" alt="Increase" /></button>
<button class="scrollBtn" id="btnQuestionDown" type="button"><img src="Images/black_downpointing_triangle.png" alt="Decrease" /></button></td>
</tr>
</table>
<div id="numberAlert"></div>
<p><strong>10: </strong><input class="questionBtn" type="button" value="Prepare Questions" name="prequestion" onClick="myClickHandler()"/></p> <!-- Prepare Questions here-->
</form>
答案 0 :(得分:0)
我已经对您的代码进行了更正,现在它的工作正常检查出来了 函数验证(){
var isDataValid = true;
var sessionNoO = document.getElementById("sessionNo");
var questionNumberO = document.getElementById("txtQuestion");
var roomTextO = document.getElementById("room");
var errSessionMsgO = document.getElementById("sessionNoAlert");
var errQuesMsgO = document.getElementById("numberAlert");
var errRoomMsgO = document.getElementById("roomAlert");
var trimmedRoomText = roomTextO.value.replace(/^\s+/, '').replace(/\s+$/, '');
if (sessionNoO.value == ""){
errSessionMsgO.innerHTML = "Please Enter in the Number of Sessions you Require";
isDataValid = false;
}else if (sessionNoO.value == 0){
errSessionMsgO.innerHTML = "Number of Sessions Must be More than 0";
isDataValid = false;
}else{
errSessionMsgO.innerHTML = "";
}
if(questionNumberO.value == 0){
errQuesMsgO.innerHTML = "Please Set the Number of Questions";
isDataValid = false;
} else {
errQuesMsgO.innerHTML = "";
}
if (roomTextO.value == ""){
errRoomMsgO.innerHTML = "Please Enter in a Room Number";
isDataValid = false;
}else if (!trimmedRoomText.length){
errRoomMsgO.innerHTML = "Please Enter in a Room Number";
isDataValid = false;
}else{
errRoomMsgO.innerHTML = "";
}
return isDataValid;
}
function submitform()
{
var sessionFormO = document.getElementById("sessionForm");
sessionFormO.submit();
}
document.getElementsByName("prequestion")[0].addEventListener('click', myClickHandler);
// problem was here
function myClickHandler()
{
if(validation())
{
showConfirm();
}
}
function showConfirm(){
var confirmMsg=confirm("Make sure that your details are correct, once you proceed after this stage you would not be able to go back and change any details towards your Session.Are you sure you want to Proceed? ");
if (confirmMsg==true)
{
submitform();
}else{
parent.close();
}
}