Javascript jQuery 不提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3117755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:19:36  来源:igfitidea点击:

jQuery doesn't submit a form

javascriptjqueryhtmlforms

提问by Danilo Bargen

I have the following HTML code:

我有以下 HTML 代码:

<?xml version="1.0" encoding="utf-8" ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
    <title>IT-Services Umfrage - Fragen</title>

    <link rel="stylesheet" type="text/css" href="/IT/umfrage/../../style.css" />
    <link rel="stylesheet" type="text/css" href="/IT/umfrage/css/umfrage.css" />

    <script type="text/javascript" src="/IT/umfrage/../../inc/jquery-1.4.2.js"></script>
    <script type="text/javascript" src="/IT/umfrage/js/umfrage.js"></script>
</head>

<body id="page-show">
    <div class="ueberschriftsbalken"><span>IT-Services Umfrage - Fragen</span></div>
    <br />
    <div id="content">

        <form action="/IT/umfrage/Umfrage/save" method="post" id="umfrageForm">
            <div class="frage radio">
                <p>1. Wie professionell empfinden Sie das Auftreten der IT-Service Anlaufstelle?</p>
                <label for="frage1_1"><input type="radio" name="frage1" id="frage1_1" value="1" /> Lausig</label><br />
                <label for="frage1_2"><input type="radio" name="frage1" id="frage1_2" value="2" /> Schwach</label><br />
                <label for="frage1_3"><input type="radio" name="frage1" id="frage1_3" value="3" /> Durchschnittlich</label><br />
                <label for="frage1_4"><input type="radio" name="frage1" id="frage1_4" value="4" /> Gut</label><br />
                <label for="frage1_5"><input type="radio" name="frage1" id="frage1_5" value="5" /> Hervorragend</label>
            </div>
            <input type="submit" value="Antworten absenden" name="submit" id="submitbutton" />
        </form>
    </div>
</body>
</html>

When clicking on the submitbutton or when pushing the enter key, the form gets submitted. But when i try $('#umfrageForm').submit(), $('form').submit()or anything similar, nothing happens. In Firebug, the function only returns the DOM path to the form element.

当点击提交按钮或按下回车键时,表单被提交。但是当我尝试$('#umfrageForm').submit()$('form').submit()或任何类似的,没有任何反应。在 Firebug 中,该函数只返回表单元素的 DOM 路径。

What could be the problem that prevents the form from being submitted?

阻止表单提交的问题可能是什么?

回答by Quentin

You have a field named submit, this clobbers the submitmethod of the form (with a reference to the DOM node for the field) that jQuery depends on in its own submitmethod.

您有一个名为 的字段submit,这破坏了submitjQuery 在其自己的submit方法中所依赖的表单方法(并引用了该字段的 DOM 节点)。

Rename it.

重命名它。

回答by Harmen

I think you fire the submit event before the DOM is actually loaded, because if I try it, this code works:

我认为您在实际加载 DOM 之前触发了提交事件,因为如果我尝试它,此代码有效:

$(document).ready(function(){

    // Submit handler, to prove everything works fine
    $('#umfrageForm').submit(function(){
        alert('hi');
        return false;
    });

    // Fire the submit event
    $('#umfrageForm').submit(); // alerts 'hi'
});

See: jsFiddle

参见:jsFiddle

回答by Harinath

yes when you have input elements with the name "submit" you will find the $('#formID').submit() to not to behave as it is expected. Just replace the input field name (may be the submit button) to something more relevant.

是的,当您输入名称为“submit”的元素时,您会发现 $('#formID').submit() 的行为不符合预期。只需将输入字段名称(可能是提交按钮)替换为更相关的内容。

I know this post is old one, just had the same problem for me and thought I should post my findings.

我知道这篇文章是旧的,只是对我有同样的问题,我认为我应该发布我的发现。

回答by Alex

I had once similar problem. Your code should work if you include Java-script file as you show. However, I suspect your code is a part of a bigger html-document. The problem arises when you download the body part of your html-document into another html-document without including Java-script-functions in sub-document (since they are included in the parent-document). Then it will not work. You have to include Java-script file also in sub-document.

我曾经有过类似的问题。如果您在展示时包含 Java 脚本文件,您的代码应该可以工作。但是,我怀疑您的代码是更大的 html 文档的一部分。当您将 html 文档的正文部分下载到另一个 html 文档中而不在子文档中包含 Java 脚本函数(因为它们包含在父文档中)时,就会出现问题。那么它就行不通了。您还必须在子文档中包含 Java 脚本文件。