2011-11-24 53 views
0

我的確認信息存在一些問題。無法獲取確認框,因爲它在我的代碼上提供錯誤

在我的代碼中包含一個確認框之前發生的事情是當我點擊一個按鈕(name = preQuestion)時,如果有任何驗證錯誤,那麼它將顯示頁面上的驗證錯誤並且不提交表單if沒有驗證錯誤,那麼它會提交表單。

但是我現在想做的事情,我不能工作,並不斷給我的錯誤是,如果有驗證錯誤,那麼它會做它現在正在做的事情,但如果沒有驗證錯誤,那麼我想要一個確認框出現......並且用戶將點擊確定或取消。如果用戶點擊確定,然後提交表單,如果用戶點擊取消,然後關閉確認框,並保持在同一頁面上。我哪裏錯了?

下面是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

找不到myClickHandler()方法定義??爲什麼你不直接調用validation()onclick –

+0

因爲我想要按鈕進行驗證,然後顯示確認框,然後提交表單,所以最好創建一個函數,在我包含showConfirm()之前從所有3個函數中檢索信息 – BruceyBandit

+0

函數的MyClickHandler完美的工作 – BruceyBandit

回答

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();  
     } 
} 
+0

已經很好的工作。我可以問一個小問題,當我點擊確定或取消時,它會關閉確認框,然後再次打開它,如果我點擊確定或第二次取消,則關閉確認框,爲什麼? – BruceyBandit

相關問題