最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Setting focus in ASP - Stack Overflow

programmeradmin1浏览0评论

I’m maintaining a site in ASP, one of the tasks is to set the focus on textbox on a page. Here is what I have tried:

<script type="text/javascript">
<!--
document.psForm['password'].focus();
//AND
document.getElementById("password").focus();
-->
</script>

I didn't think this would work... and it doesn't:

<form id="psForm" action="logonpw.asp" method="post" defaultfocus="password">

This doesn't work:

<body onload="javascript:docuument.psForm.password.focus();">

Here is the form:

<form id="psForm" action="logonpw.asp" method="post">
    <table border="0" cellpadding="5">
  <tr>
    <td>
          Password:
       </td>
    <td>
      <input type="password" name="password" value="<%= password %>" size="50">
        </td>
      </tr>
     </table>
</form>

I’m maintaining a site in ASP, one of the tasks is to set the focus on textbox on a page. Here is what I have tried:

<script type="text/javascript">
<!--
document.psForm['password'].focus();
//AND
document.getElementById("password").focus();
-->
</script>

I didn't think this would work... and it doesn't:

<form id="psForm" action="logonpw.asp" method="post" defaultfocus="password">

This doesn't work:

<body onload="javascript:docuument.psForm.password.focus();">

Here is the form:

<form id="psForm" action="logonpw.asp" method="post">
    <table border="0" cellpadding="5">
  <tr>
    <td>
          Password:
       </td>
    <td>
      <input type="password" name="password" value="<%= password %>" size="50">
        </td>
      </tr>
     </table>
</form>
Share Improve this question edited Feb 2, 2009 at 21:07 oglester 6,6558 gold badges44 silver badges65 bronze badges asked Feb 2, 2009 at 21:01 NitroxDMNitroxDM 5,15110 gold badges47 silver badges56 bronze badges 3
  • There is a typo in your body onload version – keithwarren7 Commented Feb 2, 2009 at 21:05
  • retagged, the fact that it is ASP is irrelevant – oglester Commented Feb 2, 2009 at 21:07
  • BTW, you don't need to wrap your JavaScript in ments: <!-- --> single only browsers born before 1997 will display your code instead of running it. – Diodeus - James MacFarlane Commented Feb 2, 2009 at 21:48
Add a ment  | 

4 Answers 4

Reset to default 5

Try this: Add:

id="password" 

to the input tag, then use:

document.getElementById("password").focus();

a) move

<script type="text/javascript">
<!--
document.psForm['password'].focus();
//AND
document.getElementById("password").focus();
-->
</script>

to bottom of page source.

b) fire code on load

<script type="text/javascript">
<!--
function handleOnLoad(){
document.psForm['password'].focus();
//AND
document.getElementById("password").focus();
}
-->
</script>
...
<body onload="handleOnLoad();">

and by the way, only the second onfocus would do any good.

<body onload="javascript:docuument.psForm.password.focus();">

should be

<body onload="javascript:document.psForm.password.focus();">

Check spelling...

<body onload="javascript:document.psForm.password.focus();">

document was misspelled

发布评论

评论列表(0)

  1. 暂无评论