Javascript form.submit()在Firefox中不起作用 [英] Javascript form.submit() not working in Firefox

查看:134
本文介绍了Javascript form.submit()在Firefox中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处有几个问题/答案此处这里和其他地方,但他们都看起来像JQuery具体,似乎并不适用于此(例如,我是 NOT 创建一个新的Form对象,这是文档中的一个现有表单,另外我也使用Jquery )。

There are several questions/answers on this here, here and here and elsewhere, but they all seem JQuery specific and do not appear to apply to this (for example, I am NOT creating a new Form object, this is an existing form in the document. Also I am NOT using Jquery at all).

我有一个表单,由于IE7兼容性的原因,必须在提交之前进行修改。我必须从我的表单中删除所有的 BUTTON 标签,然后添加一个隐藏字段,但这些都是现有HTML页面上现有的形式。此代码在IE和Chrome中正常工作,但在Firefox(版本23和24都经过测试)中无效。

I have a form which has to be modified before submission for reasons of IE7 compatibility. I have to strip out all the BUTTON tags from my form and then add a hidden field, but this is all in an existing form on the existing HTML page. This code works properly in IE and Chrome but doesn't work in Firefox (versions 23 & 24 both tested).

    buttonClickFunction(formName, buttonObject) {
        var formObject = document.forms[formName];
        var i = 0;

        // Strip out BUTTON objects
        for (i=0;i<formObject.length;i++) {
            if (formObject[i].tagName === 'BUTTON') {
                formObject[i].parentNode.removeChild(formObject[i]);
                i--;
            }
        }

        // Create new field
        var newField = document.createElement('input');
        newField.type = 'hidden';
        newField.id=buttonObject.id;
        newField.name = buttonObject.name;
        if (buttonObject.attributes['value'] != null) {
            newField.value = buttonObject.attributes['value'].value;
        } else {
            newField.value = buttonObject.value;
        }

        // Submit form
        formObject.appendChild(newField);
        document.forms[formName].appendChild(newField);
        document.forms[formName].submit();
    }

除了 document.forms [formName] .submit()我也尝试过 formObject.submit() - 都可以在Chrome中工作,但在Firefox中都会失败。我失败了为什么这不工作 - 我已经跟踪了JS,并观看了 document.forms [formName] .submit() execute - 没有异常出现,但没有任何事情发生在服务器上。

In addition to the document.forms[formName].submit() I have also tried formObject.submit() - both work in Chrome but both fail in Firefox. I'm at a loss as to why this doesn't work - I've traced through the JS and watched that document.forms[formName].submit() execute - no exception appears but nothing goes to the server.

任何人都可以识别为什么Firefox不会提交此表单,以及我可以如何解决?

Can anyone identify why Firefox won't submit this form, and how I can fix it?

推荐答案

Firefox希望当您提交表单时,至少有一个提交按钮可用,这意味着应该有:

Firefox expects that, when you submit a form, you have at least a submit button available, meaning there should be something like:

<button type="submit">Click me</button>

或:

<input type="submit" value="Click me" />

当您在代码中使用第一个代码时,它将无法正常工作(因为您删除所有按钮提交表格前)。当您使用第二个选项时,它也将在Firefox中运行。你可以在这个小提琴中看到: http://jsfiddle.net/q9Dzc/1/

When you use the first one in your code, it will not work (because you strip out all buttons before submitting the form). When you use the second option, it will work, also in Firefox. As you can see in this fiddle: http://jsfiddle.net/q9Dzc/1/

这篇关于Javascript form.submit()在Firefox中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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