Struts form and Javascript
I have a login form in struts and I want to have focus on the username field when the page load开发者_如何学Pythons.
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?
Assign 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;
精彩评论