Bootstrap 中的警报消息

Alert Message in Boostrap

我的注册表单在模式中,我希望只要用户没有填写表格,就必须显示 bootstrap 的警告消息。我试过搜索如何实现这个,我找到了这个。

<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Add System Roles</h4>
      </div>
      <div class="modal-body">


        @using (Html.BeginForm("Create", "SystemRoles", FormMethod.Get, new { @name = "register"})) { @Html.ValidationSummary(true)
        <div id="formAlert" class="alert hide">
          <a class="close">×</a>
          <strong>Warning!</strong> Make sure all fields are filled and try again.
        </div>
        <span class="editor-label">
                            @Html.Label("Role Name:")
                        </span>
        <span class="editor-field">
                            <input type="text" id="role_name" name="role_name" >
                        </span><text>&nbsp; &nbsp;</text>
        <button type="submit" class="btn btn-primary">Add</button> }
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-warning" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

:

$(document).ready(function() {
  //alert message implementation
  $('form[name="register"]').on("submit", function(e) {
    var role_name = $(this).find('input[name="role_name"]');
    if ($.trim(role_name.val()) === "") {
      e.preventDefault();
      $("#formAlert").slideDown(400);
    }
  });

  $(".alert").find(".close").on("click", function(e) {
    e.stopPropagation();
    e.preventDefault();
    $(this).closest(".alert").slideUp(400);
  });

});

这段代码有什么问题?为什么警报不起作用或不显示?

请帮忙。

从以下位置移除隐藏 class:

<div id="formAlert" class="alert hide">

在 jQuery 函数中添加:

$("#formAlert").hide();

我用渲染的页面做了一个演示。

$(function() {
  $("#myModal").modal();
  $("#formAlert").hide();
  $('form[name="register"]').on("submit", function(e) {
    var role_name = $(this).find('input[name="role_name"]');
    if ($.trim(role_name.val()) === "") {
      $("#formAlert").slideDown(400);
      e.preventDefault();
    }
  });

  $(".alert").find(".close").on("click", function(e) {
    e.stopPropagation();
    e.preventDefault();
    $(this).closest(".alert").slideUp(400);
  });
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>
<link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<!-- Modal -->
<div id="myModal" class="modal fade" role="dialog">
  <div class="modal-dialog">
    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Add System Roles</h4>

      </div>
      <div class="modal-body">
        <form name="register" method="post">
          <div id="formAlert" class="alert"> <a class="close">×</a>  <strong>Warning!</strong> Make sure all fields are filled and try again.</div> <span class="editor-label">
                        Role Name:
                    </span>
          <span class="editor-field">
                        <input type="text" id="role_name" name="role_name" >
                    </span>

          <text>&nbsp; &nbsp;</text>
          <button type="submit" class="btn btn-primary">Add</button>
        </form>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-warning" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

希望对您有所帮助。

Demo