【问题标题】:How to use of document.getElementByIddocument.getElementById 的使用方法
【发布时间】:2011-03-24 07:46:38
【问题描述】:

在我的 JSP 页面中,我有一个用于登录的表单,其中包含两个文本框,名称和 ID 分别为“u”代表用户名和“p”代表密码。我将这些值视为

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

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

当应用程序的第一个用户尝试使用用户作为 user1 登录并作为 pass1 传递时。这个值被传递给 admin.java,它通过 request.getParameter 获取值。

这一切都很好。在第一个用户注销后。另一个用户尝试使用身份验证用户作为 user2 再次登录并作为 pass2 传递,问题是变量 user 并传入 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;
    });
}

为什么两个变量没有取新值?我哪里出错了?

【问题讨论】:

    标签: javascript jquery


    【解决方案1】:

    我认为您的问题与我在这里提到的相同: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");再次。

    【讨论】:

    • @Archana:这解决了你的问题吗?
    • 对不起,我出站了。添加代码并没有解决我的问题。
    • 您能否详细说明您在解决方案中给出的其他选项?
    • 有人可以帮我解决这个问题吗?
    • @Arcana:对不起,我写的时候实际上是在使用另一个示例中的一些代码。在关闭函数中使用$dialog.remove() 或尝试$(this).remove()。我已将其编辑为答案。
    【解决方案2】:

    它只发生在你的盒子里吗?您为什么不检查清除临时文件和 cookie 并尝试一下。

    【讨论】:

    • 在哪个盒子里?我没有使用 cookie 并且使用过会话。在 admin.java 中,我在用户登录和注销时完成了 session.setAttribute,我已经完成了 session.removeAttribute。设置和删除后,这些值在 System.out.println 中正确打印。
    • 在 index.jsp 中,一旦填充的值不会被新值替换。
    猜你喜欢
    • 2019-02-28
    • 1970-01-01
    • 1970-01-01
    • 2016-10-23
    • 1970-01-01
    • 2014-02-16
    • 2017-04-17
    • 2014-11-02
    • 1970-01-01
    相关资源
    最近更新 更多