使用 preventdefault 后我无法提交表单

I can't get my form to submit after using preventdefault

我在模态结构中有一个表单,我从用户那里收集了一些信息,之后我想将该信息提交到 post 请求中,以便我可以在 python 中处理它。

问题是我需要先检查用户是否真的输入了信息,所以我不会得到空值。

我正在使用 .preventDefault() 来停止提交并检查值。只要我不使用 preventDefault,该程序 post 就没问题,而且在使用它时实际上不会 post。
问题是在通过所有条件后,它应该post,但没有这样做,我不知道为什么。

下面是表格的代码:

<form id="forminout" action="/in_out" method="post">
        <div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" id="incomeModal">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Add your new income</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                             <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body" id="new_incomes">
                        <div style="padding:3%">
                            <input step=".01" type="number" class="form-control input-sm" name="incoming" min="0" placeholder="0">
                            <select class="incomselect" name="incomeselects" style="height:30px;">
                                <option selected disabled>This income is coming from?</option>
                                <option value="rent">Rents</option>
                                <option value="salary">Salary</option>
                                <option value="invest_reveneu">Investments reveneus</option>
                                <option value="others">Others</option>
                            </select>
                            <select class="incomdate" name="incomedate" style="height: 30px;">
                                <option selected disabled>Incoming date</option>
                                <option value=5>1 - 5</option>
                                <option value=10>6 - 10</option>
                                <option value=15>11 - 15</option>
                                <option value=20>16 - 20</option>
                                <option value=25>21 - 25</option>
                                <option value=31>26 - 31</option>
                            </select>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-primary" id="addinbutt" type="submit">Save changes</button>
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
   </form>

工作正常,因为 post 确实发生了。

这里是 post 请求的 "simplified" 代码:

$("document").ready(function(){
    $("form").on("submit", function(e){
        errors = 0;
        e.preventDefault();
        if (errors==0)
        {
            document.getElementById("#forminout").submit();
        }
    })
})

在这种情况下errors==0条件只是作为一个简化,假设我需要检查的所有实际条件都可以。

我就是没法post。

解决方案:写 this.submit()e.target.submit() 而不是 doc.getElById().submit()


$("document").ready(function(){
    $("#forminout").on("submit", function(e){
        e.preventDefault();
        // ur conditions ...
        this.submit();
    })
})
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="forminout" action="/in_out" method="post">
        <div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" id="incomeModal">
            <div class="modal-dialog modal-lg" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title">Add your new income</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                             <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body" id="new_incomes">
                        <div style="padding:3%">
                            <input step=".01" type="number" class="form-control input-sm" name="incoming" min="0" placeholder="0">
                            <select class="incomselect" name="incomeselects" style="height:30px;">
                                <option selected disabled>This income is coming from?</option>
                                <option value="rent">Rents</option>
                                <option value="salary">Salary</option>
                                <option value="invest_reveneu">Investments reveneus</option>
                                <option value="others">Others</option>
                            </select>
                            <select class="incomdate" name="incomedate" style="height: 30px;">
                                <option selected disabled>Incoming date</option>
                                <option value=5>1 - 5</option>
                                <option value=10>6 - 10</option>
                                <option value=15>11 - 15</option>
                                <option value=20>16 - 20</option>
                                <option value=25>21 - 25</option>
                                <option value=31>26 - 31</option>
                            </select>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button class="btn btn-primary" id="addinbutt" type="submit">Save changes</button>
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    </div>
                </div>
            </div>
        </div>
   </form>

编辑:

你甚至可以根据 e 参数写这个:

$("document").ready(function(){
        $("#forminout").on("submit", function(e){
            e.preventDefault();
            // ur conditions ...
            e.target.submit();
        })
    })