简体   繁体   English

单击C#和jQuery的提交按钮时,表单不提交

[英]Form does not submit when I click submit button of it C# & jQuery

I append my select option from database table. 我从数据库表追加我的选择选项。 Select option has two type 选择选项有两种类型

  1. scalar 纯量
  2. event 事件

If the option type is scalar , I need to show 2 input fields (alarm, reset) so that user can inset values in it and when the option type is event I need to hide 2 input fields (alarm, reset) so that user cannot inset values in it. 如果选项类型为scalar ,我需要显示2个输入字段(警报,重置),以便用户可以在其中插入值;当选项类型为event时,我需要隐藏2个输入字段(警报,重置),以便用户无法插入值。

When the option type is scalar , my form saves successfully but when option type is event form submit button not going to perform any action. 当选项类型为scalar ,我的表单成功保存,但是当选项类型为event时,表单提交按钮将不执行任何操作。

Can you please help? 你能帮忙吗?

Here is my code 这是我的代码

HTML HTML

<div class="row">
            <form method="post" action="/Home/SaveTriggers" class="form-inline">
                <div class="col-md-12">
                    <div class="col-md-2">
                        <select name="sourceId" class="select2" required>
                            @foreach (var i in ViewBag.opt)
                            {
                                <option value="@i.Id,@i.name,@i.type" id="opt">@i.name</option>

                            }
                        </select>
                    </div>

                    <div class="col-md-2">
                        <input id="alarm" name="alarm" type="text" placeholder="alarm" style="display:none" required />
                    </div>
                    <div class="col-md-2">
                        <input id="reset" name="reset" type="text" placeholder="reset" style="display:none" required />
                    </div>
                    <div class="col-md-1">
                        <input type="text" name="delay" id="delay" placeholder="delay" required />
                    </div>
                    <div class="col-md-3">
                        <select class="address2" name="action" id="select" required>
                            <option selected disabled>Select alarm action</option>
                            <option>John Doe, Switch on warning light</option>
                            <option>John Doe</option>
                            <option>Do anything</option>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <button id="delete2" type="button" class="btn btn-default">Delete</button>
                    </div>


                    <button class=" add btn btn-primary" type="submit">Add</button>
                </div>
            </form>
        </div>

Jquery(as i have to hide or show alarm or reset fields respectively) jQuery(因为我必须分别隐藏或显示警报或重置字段)

<script>
        $(document).ready(function () {
            $(".select2").change(function () {
                var text = $(".select2 option:selected").text();
                var val = $(".select2 option:selected").val();
                var res = val.split(",");
                var type = res[2];

                if (type == "scalar") {

                    document.getElementById("reset").style.display = "block";
                    document.getElementById("alarm").style.display="block"


                }
                else if(type=="event") {

                    document.getElementById("reset").style.display = "none";
                    document.getElementById("alarm").style.display = "none"
                    var alarm = document.getElementById("alarm");


                }
            });
        });
    </script>

The problem is most likely your inputs for alarm and reset . 问题很可能是您输入的alarmreset If type is event , they are hidden and will most likely hold no value. 如果type是event ,则它们是隐藏的,并且很可能没有任何值。 But since they are required, the form will not submit until that error is resolved. 但是由于是必需的,因此在解决该错误之前,不会提交表单。

So you can either remove the required attribute from those 2 input fields, populate them with default values or use novalidate in your form tag. 因此,您可以从这2个输入字段中删除required属性,使用默认值填充它们,或在表单标签中使用novalidate This disables form validation. 这将禁用表单验证。 You'd have to validate your inputs by hand in javascript. 您必须手动使用javascript验证输入。

<div class="col-md-2">
    <input id="alarm" name="alarm" type="text" placeholder="alarm" style="display:none" />
</div>
<div class="col-md-2">
      <input id="reset" name="reset" type="text" placeholder="reset" style="display:none" />
</div>
 <div class="col-md-2">
                    <input id="alarm" name="alarm" type="text" placeholder="alarm" style="display:none" required />
                </div>
                <div class="col-md-2">
                    <input id="reset" name="reset" type="text" placeholder="reset" style="display:none" required />

as you're having required attribute in your input element, the validation is getting triggered and not letting you submit the form. 由于您在输入元素中具有必填属性,因此将触发验证,而不允许您提交表单。

i would suggest you to do a validation on jquery on submit instead of using default html5 validation 我建议您在提交时对jquery进行验证,而不要使用默认的html5验证

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

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