检查输入后如何显示警报?

时间:2018-04-06 15:45:40

标签: javascript jquery html bootstrap-4

我做错了什么?

检查输入表单并单击“提交”按钮后 - 如果链接不包含值,则必须出现警报。

<!DOCTYPE HTML>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>

<body>
  <div class="search-container">
    <form class="form-inline  input-group col-md-12 " method="post" name="searchForm" action="obtainSearchLink" onsubmit="return validateForm()">
      <input class="form-control" type="url" name="searchLink" placeholder="Search">
      <button type="button" class="btn btn-outline-success">Submit</button>
    </form>
  </div>
  <!--Alert must be here-->

  <div id="alert_placeholder"></div>
  <script>
    function validateForm() {
      var inputValue = document.forms["searchForm"]["searchLink"].value();
      if (!inputValue.indexOf("blablablah") >= 0) {
        $('#alert_placeholder').html('<div class="alert alert-danger alert-dismissible fade show"><button type="button" class="close" data-dismiss="alert">&times;</button>Search link is not valid!</div>')
      }
    }
  </script>
</body>

</html>

0 个答案:

没有答案