Struts plus Javascript equals error
I run Javascript in Struts,when run the statement
document.forms[0].submit()
it raise follows error,
Error:Object don't support this attribute or method
Why? I am puzzled with it for several days! I still can't find why raise error!!!
Anybody can pick up me error and tell me how to correct this error? My code is follows:
<html:form method="post" action="lo.do">
<html:select property="abc" size="1" onchange="javascript:cc()">
<html:option value="1">1</html:option>
<html:option value="2">2</html:option>
<html:submit value="search"/>
</html:form>
<script language="JavaScript">
function cc(){
document.forms[0].submit();
}
</script>
Try to close your <html:select> tag, do not specify <html:form> method attribute, do not add '.do' to your action, do not use 'javascript:' in an event and return a value :
<html:form action="lo">
<html:select property="abc" size="1" onchange="return cc();">
<html:option value="1">1</html:option>
<html:option value="2">2</html:option>
</html:select>
<html:submit value="search"/>
</html:form>
<script type="text/javascript">
function cc() {
document.forms[0].submit();
return false; // for IE
}
</script>
I use <html:submit> without any problem, even when i use events to post my form.
Hope it helps, regards
Just as a clarification: returning false from a javascript event handler cancels the current action.
While that is correct for use on a submit button (ie if validation fails, then cancel submission by returning false) I don't think it is helpful in this case. The event in question is an onchange event on a select box.
Returning false in this case would actually cancel the change event, and put the value back to what it originally was - definitely NOT the intended functionality.
Cheers,
evnafets