最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - Submit Form Via JavaScript - Stack Overflow

programmeradmin1浏览0评论

I have an html file with a form containing a submit button, and an anchor tag that is outside of the form. Is it possible to "simulate" the submit button from the anchor link?

In other words can a JavaScript function "call" submit for a form on the page?

I have an html file with a form containing a submit button, and an anchor tag that is outside of the form. Is it possible to "simulate" the submit button from the anchor link?

In other words can a JavaScript function "call" submit for a form on the page?

Share Improve this question asked Dec 19, 2010 at 18:47 JoshuaJoshua 6,84315 gold badges57 silver badges76 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 16

Sure you can use the submit() method to submit a form like this:

<a href="#" onclick="document.FormName.submit()">Submit Form</a>

Or

<a href="#" onclick="document.getElementById('formID').submit()">Submit Form</a>

To go unobstrusive, you can do this instead:

HTML:

You can give your link an id:

<a href="#" id="link">Submit Form</a>

Javascript:

<script type="text/javascript">
  var link = document.getElementById('link');

  link.onclick = function(){
    document.getElementById('formID').submit();
    return false;
  };
</script>

where formID is the id of the form.

Something like this might work... (Assuming that you give an id to your submit button.)

<a href="#" onclick="document.getElementById('theSubmitButton').click();return false;">Submit form</a>
发布评论

评论列表(0)

  1. 暂无评论