如何使用JavaScript可靠地提交HTML表单? [英] How to reliably submit an HTML form with JavaScript?

查看:115
本文介绍了如何使用JavaScript可靠地提交HTML表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以使用 JavaScript 来提交HTML表单情况如何?



我详细说明。常见的做法似乎是:

$ p $ formElement.submit()

除了一件事情,这一切都很好。表单的字段可以作为 formElement 的属性提供,因此如果有名称 ID 的字段,则text1 ,它可以被访问为 formElement.text1



这意味着如果一个元素被称为submit (无论是名称还是 id ),那么 formElement.submit()将不起作用。这是因为 formElement.submit 不是表单的方法,而是具有该名称的字段。不幸的是,提交按钮有一个提交名称或ID很常见。



两个例子来说明我的观点。首先,以下内容不起作用,因为表单的一个元素的名称为submit:

 < form name =例如id =exampleaction =/> 
< button type =buttonname =submitonclick =document.example.submit(); return false;> Submit< / button>
< / form>

尽管如此,

 < form name =exampleid这个唯一的区别就是我已经从表单中删除了提交这个名字:

=exampleaction =/>
< button type =buttononclick =document.example.submit(); return false;>提交< / button>
< / form>

那么,有没有其他方法可以使用JavaScript提交HTML表单?

解决方案

在JavaScript中创建另一个表单,并在原始表单上应用 submit()方法:

 < html> 
< script>
函数hack(){
var form = document.createElement(form);
var myForm = document.example;
form.submit.apply(myForm);
}
< / script>

< form name =exampleid =examplemethod =getaction =>
< input type =hiddenvalue =43name =hid>

form.submit 是对新鲜和干净的提交方法,然后使用 apply(myForm)以原始表单执行。


Is there a way to submit an HTML form using JavaScript that is guaranteed to work in all situations?

I elaborate. The common approach seems to be:

formElement.submit()

That is all good and well except for one thing. Fields of a form are available as attributes of formElement, so if there is a field with name or id "text1", it can be accessed as formElement.text1.

This means that if an elements is called "submit" (be it its name or its id), then formElement.submit() will not work. This is because formElement.submit won't be a method of the form, but the field with that name. Unfortunately, it's fairly common that submit buttons have a "submit" name or id.

Two examples to illustrate my point. First, the following will NOT work, because an element of the form has name "submit":

<form name="example" id="example" action="/">
  <button type="button" name="submit" onclick="document.example.submit(); return false;">Submit</button>
</form>

The following will work though. The only difference is that I have removed the name "submit" from the form:

<form name="example" id="example" action="/">
  <button type="button" onclick="document.example.submit(); return false;">Submit</button>
</form>

So, is there any other way to submit an HTML form using JavaScript?

解决方案

Create another form in JavaScript, and apply its submit() method on your original form:

<html>
    <script>
        function hack() {
            var form = document.createElement("form");
            var myForm = document.example;
            form.submit.apply(myForm);
        }
    </script>

    <form name="example" id="example" method="get" action="">
        <input type="hidden" value="43" name="hid">
        <button 
          type="button" 
          name="submit" 
          onclick="hack();return false;"
        >Submit</button>
    </form>
</html>

form.submit is the reference to a fresh and clean submit method, and then you use apply(myForm) to execute it with the original form.

这篇关于如何使用JavaScript可靠地提交HTML表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆