简体   繁体   English

如何使用document.getElementById

[英]How to use of document.getElementById

In my JSP page I have a form for login with two text boxes with name and id as "u" for username and "p" for password. 在我的JSP页面中,我有一个用于登录的表单,其中有两个文本框,名称和ID为“ u”代表用户名,“ p”代表密码。 I take in the values as 我将值视为

var user=document.getElementById("u").value; var user = document.getElementById(“ u”)。value;

var pass=document.getElementById("p").value; var pass = document.getElementById(“ p”)。value;

When the first user of the application tries to login with user as user1 and pass as pass1. 当应用程序的第一个用户尝试以用户user1身份登录并以pass1身份通过时。 This value is passed to the admin.java which gets the values by using request.getParameter. 该值被传递到admin.java,后者通过使用request.getParameter获取值。

This is all fine.After the first user logs out. 一切都很好,第一个用户注销后。 And another user tries to login again using the authetication user as user2 and pass as pass2 the problem is the variable user and pass in the index.jsp retain the same old values of user1 and pass1. 另一个用户尝试使用身份验证用户作为user2并以pass2作为密码再次登录,问题出在变量user并传递index.jsp保留了user1和pass1相同的旧值。 I checked these through alert boxes. 我通过警报框检查了这些。 The same values are passed to Admin.java. 相同的值传递到Admin.java。

The code in index.jsp is 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;
    });
}

Why is the new value not taken in the two variables? 为什么在两个变量中不采用新值? Where have I gone wrong? 我哪里出问题了?

I think your issue is the same one as I mention here: Variables doesnt refresh after AJAX request completed 我认为您的问题与我在这里提到的问题相同: AJAX请求完成后变量不会刷新

Basically, if you re-create the dialog every time, you're also recreating the IDs. 基本上,如果您每次都重新创建对话框,那么您也在重新创建ID。 This means that they are no longer unique and can cause problems with your script. 这意味着它们不再是唯一的,并且可能导致脚本问题。 Try adding a close function to your dialog script like so: 尝试向对话框脚本添加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.
});

This removes the dialog from the DOM when it is closed. 当对话框关闭时,这将从DOM中删除该对话框。

From my other post: 从我的其他帖子中:
You're other option is to save the dialog in a global variable, and then check if it has been defined yet. 您的另一选择是将对话框保存在全局变量中,然后检查它是否已定义。 If it hasn't do what you're doing. 如果没有做您正在做的事情。 If it has, set a variable to tell it what the ID is of the item you're editing and reset all the text boxes to blank before running .dialog("open"); 如果有的话,设置一个变量以告诉它您正在编辑的项目的ID是什么,并在运行.dialog(“ open”);之前将所有文本框重置为空白。 again. 再次。

Does it happen in your Box alone? 它是否仅在您的Box中发生? Why don't you check clearing temp files and cookies and having a try. 为什么不检查清除临时文件和cookie并尝试一下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM