javascript 如何使用 document.getElementById
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5416164/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use of document.getElementById
提问by Archana
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. I take in the values as
在我的 JSP 页面中,我有一个登录表单,其中包含两个文本框,名称和 ID 分别为“u”代表用户名和“p”代表密码。我将这些值视为
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. This value is passed to the admin.java which gets the values by using request.getParameter.
当应用程序的第一个用户尝试使用 user1 登录并通过 pass1 时。该值被传递给 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. I checked these through alert boxes. The same values are passed to Admin.java.
这一切都很好。在第一个用户注销后。另一个用户尝试使用身份验证用户作为 user2 再次登录并作为 pass2 传递问题是变量 user 和 index.jsp 中的传递保留了 user1 和 pass1 的相同旧值。我通过警报框检查了这些。将相同的值传递给 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?
为什么两个变量中没有采用新值?我哪里错了?
回答by Richard Marskell - Drackir
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. 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:
基本上,如果您每次都重新创建对话框,那么您也在重新创建 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.
});
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"); again.
来自我的另
一篇文章:您的另一个选择是将对话框保存在全局变量中,然后检查它是否已定义。如果它没有做你正在做的事情。如果有,请设置一个变量以告诉它您正在编辑的项目的 ID 是什么,并在运行 .dialog("open"); 之前将所有文本框重置为空白;再次。
回答by Thalaivar
Does it happen in your Box alone? Why don't you check clearing temp files and cookies and having a try.
它是否仅发生在您的 Box 中?您为什么不检查清除临时文件和 cookie 并尝试一下。