使用preventdefault后我无法提交表单

时间:2019-10-09 01:09:19

标签: javascript html forms preventdefault

我在模态结构中有一个表单,可以在其中收集用户的一些信息,之后,我希望将该信息提交到发布请求中,以便可以在python中进行处理。

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

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

以下是格式的代码:

<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>

哪个工作正常,因为该帖子实际上正在发生。

这是发布请求的“简化”代码:

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

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

我就是无法发布。

1 个答案:

答案 0 :(得分:0)

解决方案:写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();
        })
    })