c# - 填写所有要求字段后使用 Bootstrap Modal

标签 c# jquery css razor bootstrap-modal

编辑:我注意到当我点击模式内的按钮时,强制弹出窗口显示并且不允许继续。

我需要使用引导模式来添加一些关于某些授权的细节。所以,我有一个 cshtml 页面,其中包含一些必填字段和一个打开模式的按钮。但是,当我单击按钮时,即使字段为空,模式也会显示。我该如何解决这个问题?

按钮代码:

<div class="col-sm-2"><input type="submit" value="@Risorse.Language.InserisciAutorizzazione" class="btnRegister btn btn-default" data-toggle="modal" data-target="#modalLoginForm" /></div>

Modal(这不是我要使用的,我把它放在我的 cshtml 中只是为了测试):

<div class="modal fade" id="modalLoginForm" tabindex="-1" role="dialog" aria-labelledby="myModalLabel"
     aria-hidden="true">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header text-center">
                <h4 class="modal-title w-100 font-weight-bold">Sign in</h4>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body mx-3">
                <div class="md-form mb-5">
                    <i class="fas fa-envelope prefix grey-text"></i>
                    <input type="email" id="defaultForm-email" class="form-control validate">
                    <label data-error="wrong" data-success="right" for="defaultForm-email">Your email</label>
                </div>

                <div class="md-form mb-4">
                    <i class="fas fa-lock prefix grey-text"></i>
                    <input type="password" id="defaultForm-pass" class="form-control validate">
                    <label data-error="wrong" data-success="right" for="defaultForm-pass">Your password</label>
                </div>

            </div>
            <div class="modal-footer d-flex justify-content-center">
                <button class="btn btn-default">Login</button>
            </div>
        </div>
    </div>
</div>

这是图形结果:

As you can see

如您所见,弹出字段为必填字段的警告,但实际上也可能未输入。

我的想法是只有在所有必填字段都已填写时才通过 JS 打开模式。但我想知道是否有更优雅的方法。

谢谢。

最佳答案

使用以下代码在验证之前阻止模型,

HTML:

<input type="text" id="txt_name" /><button type="button" class="btn btn-primary btn-lg no-modal" data-toggle="modal" data-target="#myModal">Launch demo modal</button>

Jquery:

$('#myModal').on('show.bs.modal', function (e) {
var button = e.relatedTarget;
if($("#txt_name").val().length==0) {
e.stopPropegation();
}  
});

关于c# - 填写所有要求字段后使用 Bootstrap Modal,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59649450/

相关文章:

c# - 使用 CaSTLe Windsor 的 WCF 依赖注入(inject) - 请帮忙?

javascript - '未捕获的语法错误 : Unexpected token x in JSON at position 1' when trying to parse HTML embeded string JSON

javascript - jQuery Tabs 使用 Mobify 在 Carousel 上激活

c# - 编辑后未清除 WPF Datagrid 行验证错误

c# - 使用 Bloomberg API 时 session 未在 C# 中启动

c# - 在 Controller 的构造函数中加载所有模型项?

javascript - 如何使用 Javascript/Jquery 重定向到特定窗口宽度的页面?

jQuery : validate date

html - 我怎样才能把它变成列表元素中的两列布局?

css - 更改与背景重叠的部分文本的颜色