javascript - AJAX 表单提交 - 每次点击都会提交表单 i++ 次

标签 javascript ajax forms

我遇到一个问题,第一次使用 AJAX 提交表单工作正常,但如果我再次单击提交按钮,或按 Enter 键,表单会提交两次。第三次单击会导致表单提交 3 次,依此类推。我尝试在 scripts.js 文件中插入 return falsepreventDefault() 但无济于事。

可以找到我的表单的实现 here可以找到输出的结果列表 here

此外,我注意到 scripts.js 永远不会继续到 document.getElementById('test').innerHTML="this Works3";

任何帮助将不胜感激。谢谢!

index.html

<form id="form1" action="addpost.php" method="post">
    <div class="form-group" >
        <label for="title">Title:</label>
        <input name="title" type="title" class="form-control " id="title" placeholder="Enter title (Compulsory)" required>
        <span class="help-block">This is some help text that breaks onto a new line and may extend more than one line.</span>
    </div>
    <div class="form-group">
        <label for="name">Name:</label>
        <input type="name" class="form-control" id="name">
        <span class="help-block">This is some help text that breaks onto a new line and may extend more than one line.</span>
    </div>
    <div class="form-group">
        <label for="article">Article:</label>
        <textarea name="article" class="form-control" rows="5" id="article"></textarea>
    </div>
    <div class="form-group">
        <label for="img1">Image 1:</label>
        <input type="file" class="form-control" id="img1" accept="image/*">
        <span class="help-block">This is some help text that breaks onto a new line and may extend more than one line.</span>
    </div>
    <label id="test">hi</label>
    <button class="btn btn-primary" onclick="poop()">Button</button>
    <input type="submit" class="btn btn-primary" onclick="poop()" value = "submit"/>
</form> 

scripts.js

function poop()
{
    document.getElementById('test').innerHTML="this works";
    var form = $('#form1');
    document.getElementById('test').innerHTML="this works2";
    form.submit(function (event) 
    {
        $.ajax
        ({
            type: form.attr('method'),
            url: form.attr('action'),
            data: form.serialize(),
            success: function (data) 
            {
                alert('ok');
            }
        });
    });
    document.getElementById('test').innerHTML="this works3";

}

最佳答案

感谢 Liam 和 Vinod 为我指明了正确的方向。我在index.html 文件中包含了覆盖提交函数的脚本。很好地解决了这个问题。

index.html

        <form id="form1" action="addpost.php" method="post">
            <div class="form-group" >
                <label for="title">Title:</label>
                <input name="title" type="title" class="form-control " id="title" placeholder="Enter title (Compulsory)" required>
                <span class="help-block">This is some help text that breaks onto a new line and may extend more than one line.</span>
            </div>
            <div class="form-group">
                <label for="name">Name:</label>
                <input type="name" class="form-control" id="name">
                <span class="help-block">This is some help text that breaks onto a new line and may extend more than one line.</span>
            </div>
            <div class="form-group">
                <label for="article">Article:</label>
                <textarea name="article" class="form-control" rows="5" id="article"></textarea>
            </div>
            <div class="form-group">
                <label for="img1">Image 1:</label>
                <input type="file" class="form-control" id="img1" accept="image/*">
                <span class="help-block">This is some help text that breaks onto a new line and may extend more than one line.</span>
            </div>
            <label id="test">hi</label>
            <input type="submit" class="btn btn-primary" value = "submit"/>
        </form> 

// script included within body of html
<script type="text/javascript">
        var form = $('#form1');
        document.getElementById('test').innerHTML="this works2";
        form.submit(function (event) 
        {
            event.preventDefault();
            $.ajax
            ({
                type: form.attr('method'),
                url: form.attr('action'),
                data: form.serialize(),
                success: function (data) 
                {
                    alert('ok');
                }
            });
        });
</script>

关于javascript - AJAX 表单提交 - 每次点击都会提交表单 i++ 次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40673550/

相关文章:

javascript - Webpack - 捆绑 js 中未缩小的文件

jquery - Ajax/jQuery 问题

ajax - ASP.NET MVC-4 : What should receive an ajax call

JavaScript AJAX 库通过 JSONP 使用 WS REST

javascript - 当多个字段中的任何一个发生变化时触发javascript函数(onchange)

javascript 风格——我应该定义变量/类吗?

javascript - jQuery 过滤器和突出显示表

javascript - 将动态字符串传递给对象

python - 在 django CreateView 中将 args 和 kwargs 传递给具有额外内容的父类

forms - 重置表单和验证