2010-08-01 11 views
80

Sto provando a impostare lo stato attivo predefinito su una casella di input quando la pagina viene caricata (esempio: google). La mia pagina è molto semplice, ma non riesco a capire come farlo.Impostazione dello stato attivo su una casella di input HTML al caricamento della pagina

Questo è quello che ho finora:

<html> 
<head> 
<title>Password Protected Page</title> 

<script type="text/javascript"> 
function FocusOnInput() 
{ 
document.getElementById("PasswordInput").focus(); 
} 
</script> 

<style type="text/css" media="screen"> 
    body, html {height: 100%; padding: 0px; margin: 0px;} 
    #outer {width: 100%; height: 100%; overflow: visible; padding: 0px; margin: 0px;} 
    #middle {vertical-align: middle} 
    #centered {width: 280px; margin-left: auto; margin-right: auto; text-align:center;} 
</style> 
</head> 
<body onload="FocusOnInput()"> 
<table id="outer" cellpadding="0" cellspacing="0"> 
    <tr><td id="middle"> 
    <div id="centered"> 
    <form action="verify.php" method="post"> 
    <input type="password" name="PasswordInput"/> 
    </form> 
    </div> 
    </td></tr> 
</table> 
</body> 
</html> 

Come mai questo non funziona, mentre questo funziona bene?

<html> 
<head> 
<script type="text/javascript"> 
function FocusOnInput() 
{ 
    document.getElementById("InputID").focus(); 
} 
</script> 
</head> 

<body onload="FocusOnInput()"> 
    <form> 
     <input type="text" id="InputID"> 
    </form> 
</body> 

</html> 

aiuto è molto apprezzato :-)

risposta

33

Questa linea: dovrebbe avere un attributo id , in questo modo

<input type="password" name="PasswordInput"/> 

:

<input type="password" name="PasswordInput" id="PasswordInput"/> 
271

ed è possibile utilizzare L'attributo autofocus di HTML5 (funziona in tutti i browser correnti tranne IE9 e bel OW). Chiama il tuo script solo se è IE9 o precedente o una versione precedente di altri browser.

<input type="text" name="fname" autofocus> 
+24

“funziona in tutti i browser attuali” - che dipende davvero dalla tua definizione di "tutti i browser correnti". –

+7

ie9: http://stackoverflow.com/questions/8280988/how-to-make-input-autofocus-in-internet-explorer – jedierikb

+0

@BhaveshGangani ['autofocus' è non supportato in Internet Explorer 9 e versioni precedenti.] (http://www.w3schools.com/tags/att_input_autofocus.asp) –

0

Si potrebbe anche usare:

<body onload="focusOnInput()"> 
    <form name="passwordForm" action="verify.php" method="post"> 
     <input name="passwordInput" type="password" /> 
    </form> 
</body> 

E poi nel tuo JavaScript:

function focusOnInput() { 
    document.forms["passwordForm"]["passwordInput"].focus(); 
} 
2

questo è uno dei problemi più comuni con IE e correzione per questo è semplice. Aggiungi .focus() due volte all'ingresso.

Fix: -

function FocusOnInput() { 
    var element = document.getElementById('txtContactMobileNo'); 
    element.focus(); 
    setTimeout(function() { element.focus(); }, 1); 
} 

E chiama FocusOnInput() su $ (document) .ready (function() {} .....;

Problemi correlati