Text link won't submit form by JavaScript in S

2019-07-23 12:41发布

问题:

I'm using Safari 5.0.3 I have a form in my html:

<form name="searchForm" style="margin-top:24px;">
<h4 style="float:left;margin-top:-1px;">Search</h4> 
&nbsp; <input type="text" name="keywords" id="keywords"></input> &nbsp; <a href="#" onClick="document.forms[0].submit(); return false">Go ></a>
</form>

This works fine:

a href="#" onClick="document.forms[0].submit(); return false"

but this does nothing:

a href="#" onClick="document.forms['searchForm'].submit(); return false"

I need to use the latter, because the page template is dynamic, and sometimes there will be a form in the page before this one.

回答1:

Use: document.getElementById('searchForm').submit() instead.



回答2:

Try this out:

<a href="#" onClick="document.searchForm.submit(); return false;">

If that doesn't work, Macy Abbey has a great idea of adding an id to the form and referencing it by that.