“提交不是函数” Firefox中动态创建表单时出错,无需其他提交输入 [英] "Submit is not a function" error in Firefox in dynamically CREATED form without other submit inputs

查看:119
本文介绍了“提交不是函数” Firefox中动态创建表单时出错,无需其他提交输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firefox 5的错误控制台中(但不在IE 9中),当我调用以下JavaScript函数(在外部脚本文件中)时,出现错误myForm.submit不是函数:

 函数go(url_go,arr_POST_vars,str_method){
var str_method = str_method || POST; //默认使用POST
var myForm = document.createElement(form_redir);
myForm.setAttribute(method,str_method);
myForm.setAttribute(action,url_go);
for(var key in arr_POST_vars){
var myInput = document.createElement(input);
myInput.setAttribute(name,key);
myInput.setAttribute(type,hidden);
myInput.setAttribute(value,arr_POST_vars [key]);
myForm.appendChild(myInput);
}
document.body.appendChild(myForm);
myForm.submit();
}

我的页面上唯一的HTML(在HTML body标签之间)如下line:

 < button type ='button'onClick ='javascript:go(http://www.testsite。 com / login.php,{userCode:2487,password:jon123},POST);'> Go!< / button> 

我已经Google广泛搜索,并且我知道这个错误发生在有一个表单输入时命名为submit,然后与表单的submit()函数名冲突。然而,这不是这种情况。



这里的想法是通过动态创建表单来导航并将POST值提交到特定网址,当点击按钮时。它在IE中运行正常,但不是Firefox。



我很茫然,一个解决方案将深受赞赏。 h2_lin>解决方案

它是由您创建的元素类型引起的。 form_redir不是有效的html标签,所以它创建了一个元素,但它只有默认的dom方法。将其更改为form即可使用。


In Firefox 5's error console (but not in IE 9) I get the error "myForm.submit is not a function" when I call the following javascript function (in an external script file):

function go(url_go, arr_POST_vars, str_method) {
  var str_method = str_method || "POST";    // by default uses POST
  var myForm = document.createElement("form_redir");
  myForm.setAttribute("method", str_method);
  myForm.setAttribute("action", url_go);
  for (var key in arr_POST_vars) {
    var myInput = document.createElement("input");
    myInput.setAttribute("name", key);
    myInput.setAttribute("type", "hidden");
    myInput.setAttribute("value", arr_POST_vars[key]);
    myForm.appendChild(myInput);
  }
  document.body.appendChild(myForm);
  myForm.submit();
}

The only HTML on my page (between the HTML body tags) is the following line:

<button type='button' onClick='javascript:go("http://www.testsite.com/login.php", {userCode:"2487",password:"jon123"}, "POST");'>Go!</button>

I have Googled extensively, and I know that this error occurs when there is a form input which is also named "submit", which then clashes with the "submit()" function name of the form. However this is not the case here.

The idea here is to navigate and submit POST values to a specific url when the button is clicked, by creating a form dynamically. It works fine in IE but not Firefox.

I am at a loss and a solution will be deeply appreciated.

解决方案

Its caused in the type of element you've created. "form_redir" is no valid html tag so it created an element but it has only the default dom methods. Change it to "form" and it will work.

这篇关于“提交不是函数” Firefox中动态创建表单时出错,无需其他提交输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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