【问题标题】:How to validate textbox using bootstrap banner如何使用引导横幅验证文本框
【发布时间】:2019-04-24 03:30:34
【问题描述】:

我对 javascript 比较陌生,我正在尝试创建一个横幅,如果文本框为空,将显示该横幅。但它没有显示。如何使用引导横幅发出警报?

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>

<script type="text/javascript">

function validateForm() {
      var uname = document.getElementById("uname").value;
      if (uname == ""){
          bootstrap_alert.warning = function(message) {
            $('#alert_placeholder').html('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><span>'+message+'</span></div>')
        }

$('#clickme').on('click', function() {
            bootstrap_alert.warning('Please enter your name');
});
          return false;
      }
  }
</script>
</head>
<body>
Name: <input type: "text" name="uname" id="uname" />
<input type = "button" id = "clickme" value="Submit"/>
<div id = "alert_placeholder"></div>

</body>
</html>

【问题讨论】:

    标签: javascript jquery html twitter-bootstrap bootstrap-modal


    【解决方案1】:

    在编写 JS 代码时,您应该考虑几件事:

    1. 如果你想要你的 Jquery,总是包含$(document).ready(function(){}); 等待html正文加载的代码。一旦它被加载你想要 使用你的 JS 代码。
    2. 另一件事是您创建了一个validateform(),它永远不会从您的代码中调用,并且您的主代码存在于该函数中。

    我已经修改了您的代码以完美运行

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
    
    <script>
    $( document ).ready(function() {
    
     $('#clickme').on('click', function() {
                  bootstrap_alert('Please enter your name');
        });
          
               bootstrap_alert = function(message) {
              var uname=$("#uname").val();
          		if (uname.length==0){
                $('#alert_placeholder').html('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><span>'+message+'</span></div>')
            }
            else{
            $('#alert_placeholder').html('');
            }
        }
    });
    </script>
    </head>
    <body>
    Name: <input type="text" name="uname" id="uname" />
    <input type = "button" id = "clickme" value="Submit"/>
    <div id = "alert_placeholder"></div>
    
    </body>
    </html>

    如果您在这方面需要任何帮助,请告诉我。我很乐意为您提供帮助。谢谢

    【讨论】:

    • 如果我有多个文本框,我希望每个文本框都有一个错误框?
    • 如果你想在有多少个输入为空的情况下都显示一个错误,那么这就足够了.. 但是如果你想让每个文本框都有一个错误,那么一种解决方案是创建一个动态 div 并在 jquery 中使用 after() 函数将其附加到特定输入框之后。
    • 你能举个例子吗?
    • 我建议你提出另一个问题,我会在那里回答,因为这是一个有点不同的问题,如果你想回答她,请告诉我。我去的时候会回答home.. 现在我正忙于其他事情
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 2021-02-16
    相关资源
    最近更新 更多