2011-03-24 162 views
1

在我的JSP頁面中,我有一個登錄表單,其中有兩個文本框,名稱和標識分別爲用戶名「u」和密碼「p」。我認爲值爲如何使用document.getElementById

var user = document.getElementById(「u」).value;

var pass = document.getElementById(「p」).value;

當應用程序的第一個用戶嘗試以user1身份登錄user並以pass1身份傳遞時。這個值傳遞給admin.java,通過使用request.getParameter獲取值。

這很好。第一個用戶註銷後。另一個用戶嘗試使用認證用戶作爲user2再次登錄,並以pass2身份通過,問題在於變量用戶,並在index.jsp中傳遞,保留user1和pass1的舊值。我通過警報框檢查了這些。相同的值傳遞給Admin.java。

中的index.jsp的代碼

function prepareLoginDialog(){ 
    $dialog = $('<div></div>') 
    .html('<div id="dialog-confirm" title="Enter the login details here :">'+ 
     'User: <input type="text" id="u" name="u" ></input><br />'+ 
     'Pass: <input type="password" id="p" name="p" ></input>'+ 
     '</div>') 

     //System.out.println(user); 
    //System.out.println(pass); 
    .dialog({ 
     autoOpen: false, 
     modal: true, 
     title: 'Login', 
     buttons: { 
      'Login': function() { 
       //Send Login Request to Server 
       alert("I in login"); 

       var user = document.getElementById("u").value; 
       var pass = document.getElementById("p").value; 
       alert(user); 
       alert(pass); 
       //System.out.println(u.text); 
       login(user,pass); 

       $(this).dialog('close'); 
      }, 
      Cancel: function() { 
       $(this).dialog('close'); 
      } 
     } 
    }); 

    $('#login').click(function() { 
     $dialog.dialog('open'); 
     // prevent the default action, e.g., following a link 
     return false; 
    }); 
} 

爲什麼新的價值不是在兩個變量取?我哪裏錯了?

回答

3

我覺得你的問題是同一個,因爲我在這裏提到:Variables doesnt refresh after AJAX request completed

基本上,如果你重新創建對話框每一次,你也重新創建的ID。這意味着它們不再是唯一的,並可能導致腳本出現問題。嘗試添加close函數到您的對話框腳本像這樣:

.dialog({ 
    autoOpen: false, 
    modal: true, 
    title: 'Login', 
    buttons: { 
     'Login': function() { 
      //Send Login Request to Server 
      alert("I in login"); 

      var user = document.getElementById("u").value; 
      var pass = document.getElementById("p").value; 
      alert(user); 
      alert(pass); 
      //System.out.println(u.text); 
      login(user,pass); 

      $(this).dialog('close'); 
     }, 
     Cancel: function() { 
      $(this).dialog('close'); 
     } 
    }, 
    close: function() { $dialog.remove(); } //$(this).remove(); Might also work. 
}); 

這從DOM中刪除的對話框時,它被關閉。

從我的其他文章:
你還有其他選擇是將對話框保存在一個全局變量中,然後檢查它是否已被定義。如果它沒有做你在做什麼。如果有,請設置一個變量以告訴它正在編輯的項目的ID,並在運行.dialog(「open」)之前將所有文本框重置爲空白。再次。

+0

@Archana:這是否解決了您的問題? – 2011-03-26 15:11:42

+0

對不起,我站外了。添加代碼並沒有解決我的問題。 – Archana 2011-03-30 04:36:10

+0

您可以詳細說明您在解決方案中提供的其他選項嗎? – Archana 2011-03-30 08:06:28

0

它是否發生在你的盒子裏?你爲什麼不檢查清理臨時文件和cookies並嘗試一下。

+0

在哪個箱子?我沒有使用Cookie並使用過會話。在admin.java中,當用戶登錄並註銷時,我完成了session.setAttribute,並完成了session.removeAttribute。這些值在設置和刪除後都可以正確地在System.out.println中打印。 – Archana 2011-03-24 09:20:50

+0

它在index.jsp中曾經填充過的值沒有被新值取代。 – Archana 2011-03-24 09:21:50

相關問題