更新:我搞定了。我添加了按键事件处理程序:
function keyHit(evt) {
var e = evt || window.event;
if ( e.keyCode == 13 ) {
blnPwdBoxWait = 'DONE';
}
return;
}
并将以下行添加到我的密码提示功能:
objIE.document.addEventListener( "keydown", keyHit, false );
等待发生事情的循环现在看起来像这样:
try {
do {
WScript.Sleep( 100 );
if ( objIE.Visible && blnPwdBoxWait == '' ) {
blnPwdBoxWait = objIE.document.getElementById( "submitted" ).value;
}
} while ( blnPwdBoxWait == '' );
} catch( err ) {
WScript.Echo('ERROR: ' + err.message);
blnPwdBoxWait = 'DONE';
}
最让我感到困惑的是我(通过使用WScript.Echo语句)证明事件处理程序正在捕获键击,但循环没有退出。我终于意识到我需要改变这条线
if ( objIE.Visible ) {
到
if ( objIE.Visible && blnPwdBoxWait == '' ) {
保持标志变量blnPwdBoxWait在按下Enter键但未单击“提交”按钮时被覆盖为“”。一旦我做了那个修复,它就像一个魅力。谢谢你的帮助!
ORIGINAL POST:我有一个Javascript函数,它使用Internet Explorer打开一个窗口,提示用户输入他的登录ID,密码和目标服务器,并显示“提交”按钮。它可以工作,但只有在单击“提交”按钮时,按下Enter键时它才会执行任何操作。我尝试了一些常见的事情,比如在表单标记中添加一个action属性,但是我尝试过的任何东西似乎都没有用。任何人都可以告诉我,当按下Enter键时,我应该怎么做才能使函数提交其值?这是一个包含该功能的小型演示:
True = 1;
False = 0;
aryServers = Array( "server1", "server2", "server3", "server4" );
strDefaultServer = "server2";
objWShell = new ActiveXObject( "WScript.Shell" );
strLocalUser = objWShell.ExpandEnvironmentStrings( "%USERNAME%" );
strPassword = PasswordBox( "Enter Credentials" );
WScript.Echo( "Login ID: " + strLoginID + "\nPassword: " + strPassword +
"\nServer: " + strServer );
WScript.Exit;
// ===========================================================================
function PasswordBox( strIETitle ) {
objIE = new ActiveXObject( "InternetExplorer.Application" );
objIE.FullScreen = False;
objIE.AddressBar = False;
objIE.MenuBar = False;
objIE.StatusBar = False;
objIE.ToolBar = False;
objIE.RegisterAsDropTarget = False;
objIE.Navigate("about:blank");
strLoginID = strLocalUser;
do {
WScript.Sleep( 100 );
} while ( ! objIE.ReadyState == 4 );
objIE.document.parentWindow.resizeTo( 400, 300 + 70 );
objIE.document.parentWindow.resizeTo( 400, 200 + 70 );
objIE.document.parentWindow.moveTo(
objIE.document.parentWindow.screen.width / 2 - 200,
objIE.document.parentWindow.screen.height / 2 - 200 );
objIE.document.writeln( "<html>" );
objIE.document.writeln( "<head>" );
objIE.document.writeln( "<title>" + strIETitle + "</title>" );
objIE.document.writeln( "<style type='text/css'>" );
objIE.document.writeln( "<!--" );
objIE.document.writeln( ".fixed { font-family:courier new, monospace }" );
objIE.document.writeln( "-->" );
objIE.document.writeln( "</style>" );
objIE.document.writeln( "</head>" );
objIE.document.writeln( "<body bgcolor=Silver>" );
objIE.document.writeln( "<center>" );
objIE.document.writeln( "<form>" );
objIE.document.writeln( "<b>" + strIETitle + "</b><p>" );
objIE.document.writeln( "<table>" );
objIE.document.writeln( "<tr><td colspan=2 align=left>" );
objIE.document.writeln( "Enter your username and password:<br>" );
objIE.document.writeln( "</td></tr><tr><td valign=top>" );
objIE.document.writeln( "Username: " );
objIE.document.writeln( "</td><td>" );
objIE.document.writeln( "<input id='userid' size=20 class='fixed' " +
"value='" + strLoginID + "'>" );
objIE.document.writeln( "</td></tr><tr><td valign=top>" );
objIE.document.writeln( "Password: " );
objIE.document.writeln( "</td><td>" );
objIE.document.writeln( "<input type='password' id='passwd' size=20 class='fixed'><p>" );
objIE.document.writeln( "</td></tr><tr><td valign=top>" );
objIE.document.writeln( "Remote Host: " );
objIE.document.writeln( "</td><td valign=top>" );
objIE.document.writeln( "<select id='server'><br>" );
intLen = aryServers.length;
for ( intI = 0; intI < intLen; intI++ ) {
if ( strDefaultServer == aryServers[ intI ] ) {
objIE.document.writeln( "<option value='" + aryServers[ intI ] +
"' selected>" + aryServers[ intI ] + "<br>" );
} else {
objIE.document.writeln( "<option value='" + aryServers[ intI ] +
"'>" + aryServers[ intI ] + "<br>" );
}
}
objIE.document.writeln( "</select>" );
objIE.document.writeln( "</td></tr>" );
objIE.document.writeln( "</table>" );
objIE.document.writeln( "<p>" );
objIE.document.writeln( "<input type='button' value='Submit' id='but0' " +
"onclick=\"submitted.value='DONE';\">" );
objIE.document.writeln( "<input type='hidden' id='submitted' value=''>" );
objIE.document.writeln( "</form>" );
objIE.document.writeln( "</center>" );
objIE.document.writeln( "</body>" );
objIE.document.writeln( "</html>" );
objIE.document.parentWindow.document.body.scroll="no";
objIE.document.parentWindow.document.body.style.borderStyle = "outset";
objIE.document.parentWindow.document.body.style.borderWidth = "3px";
objIE.document.getElementById( "passwd" ).focus();
objIE.Visible = True;
objWShell.AppActivate( strIETitle );
blnPwdBoxWait = '';
try {
do {
WScript.Sleep( 100 );
if ( objIE.Visible ) {
blnPwdBoxWait = objIE.document.getElementById( "submitted" ).value;
}
} while ( blnPwdBoxWait=='' );
} catch( err ) {
WScript.Echo('ERROR: ' + err.message);
blnPwdBoxWait == 'DONE';
}
strLoginID = objIE.document.getElementById( "userid" ).value;
strPassword = objIE.document.getElementById( "passwd" ).value;
strServer = objIE.document.getElementById( "server" ).options( objIE.document.getElementById( "server" ).selectedIndex ).text;
objIE.Visible = False;
objIE.Quit();
objIE = null;
return strPassword;
}