简体   繁体   English

如果在动态 html 上无效,则阻止表单提交

[英]prevent form from submitting if invalid on dynaimic html

I have a .net core view which has a for loop less than 6 and then generates some html table rows which therein contains a select list and input type number with maxlength=600 and max=3.我有一个 .net 核心视图,它有一个小于 6 的 for 循环,然后生成一些 html 表行,其中包含一个选择列表和输入类型编号,maxlength=600 和 max=3。

Whats happening is on the 1st row when i satisfy all values and submit the form posts to the server as ideally.当我满足所有值并理想地将表单发布到服务器时,发生的事情发生在第一行。

But when also the 2nd row is populated with in-valid values the form still submits但是当第二行也填充了无效值时,表单仍然提交

How can I prevent the form from submitting if invalid values are captured.如果捕获到无效值,如何防止表单提交。

My current code does:我当前的代码是:

 <tbody>
                                            @for (@i = 1; @i < 6; @i++)
                                            {
                                                if (@i == 1)
                                                {
                                                    required = "required";
                                                    selected = "selected";
                                                }
                                                else
                                                {
                                                    required = string.Empty;
                                                    selected = string.Empty;
                                                }
                                                if (@i == 1)
                                                {
                                                    <tr class="rowClass">
                                                        <td><label id="txtLineNo-@i">@i</label></td>
                                                        <td><input type="number" class="form-control myClass" id="@("txtQty"+i)" name="Qty" data-rule-digits="true" @required /></td>
                                                        <td>
                                                            <select class="js-select2-custom custom-select myClass" name="Description" size="1" id="@("txtDescription"+i)" @required
                                                                    data-hs-select2-options='{ "minimumResultsForSearch": "Infinity","placeholder": "Load" }'>
                                                                <option label="empty"></option>
                                                                <option value="Boxes" selected>Boxes</option>
                                                                <option value="Envelop">Envelop</option>
                                                                <option value="Pallet">Pallet</option>
                                                            </select>

                                                        </td>
                                                        <td><input type="number" class="form-control myClass" maxlength="3" id="@("txtLength"+i)" name="Length" data-rule-digits="true" max="600" data-msg="Max length 600cm" @required /></td>
                                                        <td><input type="number" class="form-control myClass" maxlength="3" id="@("txtBreadth"+i)" name="Breadth" data-rule-digits="true" max="250" data-msg="Max breadth 250cm" @required /></td>
                                                        <td><input type="number" class="form-control myClass" maxlength="3" id="@("txtHeight"+i)" name="Height" data-rule-digits="true" max="250" data-msg="Max height 250cm" @required /></td>
                                                        <td><input type="number" class="form-control myClass" maxlength="3" id="@("txtMass"+i)" name="Mass" data-rule-digits="true" @required /></td>
                                                    </tr>
                                                }
                                                else
                                                {
                                                    <tr class="rowClass">
                                                        <td><label id="txtLineNo-@i">@i</label></td>
                                                        <td><input type="number" class="form-control myClass" id="@("txtQty"+i)" name="Qty" data-rule-digits="true" @required /></td>
                                                        <td>
                                                            <select class="js-select2-custom custom-select myClass" name="Description" size="1" id="@("txtDescription"+i)" @required
                                                                    data-hs-select2-options='{ "minimumResultsForSearch": "Infinity","placeholder": "Load" }'>
                                                                <option label="empty"></option>
                                                                <option value="Boxes">Boxes</option>
                                                                <option value="Envelop">Envelop</option>
                                                                <option value="Pallet">Pallet</option>
                                                            </select>

                                                        </td>
                                                        <td><input type="number" class="form-control myClass" maxlength="3" id="@("txtLength"+i)" name="Length" data-rule-digits="true" max="600" data-msg="Max length 600cm" @required /></td>
                                                        <td><input type="number" class="form-control myClass" maxlength="3" id="@("txtBreadth"+i)" name="Breadth" data-rule-digits="true" max="250" data-msg="Max breadth 250cm" @required /></td>
                                                        <td><input type="number" class="form-control myClass" maxlength="3" id="@("txtHeight"+i)" name="Height" data-rule-digits="true" max="250" data-msg="Max height 250cm" @required /></td>
                                                        <td><input type="number" class="form-control myClass" maxlength="3" id="@("txtMass"+i)" name="Mass" data-rule-digits="true" @required /></td>
                                                    </tr>
                                                }
                                            }
                                        </tbody>

Last but not least my jquery function to prevent form submit:最后但并非最不重要的是我的 jquery 函数来防止表单提交:

$('#btnSave').on('click', function () {
                    $("tr .myClass").each(function (e) {
                    if ($(this).hasClass('is-invalid' == true)) {
                        return false; //break out of .each     
                        e.preventDefault();
                        console.log('invalid')
                    }
                });
                debugger;
})

Try this:尝试这个:

$('#btnSave').on('click', function(e) {
    $("tr .myClass").each(function(index,element) {
      if ($(element).hasClass('is-invalid' == true)) {    
        e.preventDefault();
        console.log('invalid');
        return false; //break out of .each 
      }
});
debugger;

}) })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM