admin管理员组文章数量:1426931
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;
本文标签: javaStruts form and JavascriptStack Overflow
版权声明:本文标题:java - Struts form and Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745468641a2659645.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论