I have a login form in struts and I want to have focus on the username field when the page loads.
I am using <html:form>
tag and I cannot get this form using its name from javascript as document.formName.username .
How can I do this?
I have a login form in struts and I want to have focus on the username field when the page loads.
I am using <html:form>
tag and I cannot get this form using its name from javascript as document.formName.username .
How can I do this?
Share Improve this question asked Mar 24, 2011 at 13:32 Muhammad Imran TariqMuhammad Imran Tariq 23.4k47 gold badges129 silver badges191 bronze badges 1- You should also paste the relevant code, i.e. your form with the textfield on which you're trying to transfer the focus to. – asgs Commented Mar 25, 2011 at 12:47
3 Answers
Reset to default 1Assign an id to the input element that you want to focus on, then do:
document.getElementById("myInputId").focus();
Alternately, don't give it an id, and do:
document.getElementsByName("username")[0].focus();
We cannot assign id to the form like
/so the best way to do this is to access a particular form in a sequence it appears. Same is the sequence case for form elements.
document.forms[0].elements[0]
if you have <html:form action="/someAction">
and in your struts-config.xml
, you've specified a name
for that action (which points to an ActionForm
) and your html declaration is <html:html xhtml="true">
and inside your form you have <html:input>
(with name of "userName"), then you can do this in your javascript:
document.formName.userName.value;
else
document.forms["formName"].userName.value;
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745468642a4629029.html
评论列表(0条)