通过JavaScript提交表单

通过JavaScript提交表单

本文介绍了通过JavaScript提交表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个HTML文件,其中包含一个提交按钮的表单和一个不在表单之外的锚标签。是否有可能从锚链接模拟提交按钮?



换句话说,JavaScript函数call可以提交页面上的表单吗?

p>

解决方案

当然,您可以使用方法来提交这样的表单:

 < a href =#onclick =document.FormName.submit()>提交表单< / a> 



 < a href =#onclick =document.getElementById('formID')。submit()>提交表单< / a> 

为了避免碰撞,您可以这样做:



HTML:

您可以为链接指定一个ID:

 < a href =#id =link>提交表单< / a> 

Javascript:

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

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

其中 formID 是<$ c $ 表格的c> id 。


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?

解决方案

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.

这篇关于通过JavaScript提交表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 22:51