javascript - 使用 Javascript 进行 Bootstrap 表单验证

标签 javascript twitter-bootstrap validation

我有一个 Bootstrap 表单,我想通过单击链接(而不是提交按钮)使用 Javascript 进行验证。这是我的示例代码。

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">

    <title></title>
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <style>
      /* ==========================================================================
          Demo using Bootstrap 3.3.4 and jQuery 1.11.2
          You don't need any of the following styles for the form to work properly, 
          these are just helpers for the demo/test page.
        ========================================================================== */

      #wrapper { 
        width:595px;
        margin:0 auto;
      }
      legend {
        margin-top: 20px;
      }
      #attribution {
        font-size:12px;
        color:#999;
        padding:20px;
        margin:20px 0;
        border-top:1px solid #ccc;
      }
      #O_o { 
        text-align: center; 
        background: #33577b;
        color: #b4c9dd;
        border-bottom: 1px solid #294663;
      }
      #O_o a:link, #O_o a:visited {
        color: #b4c9dd;
        border-bottom: #b4c9dd;
        display: block;
        padding: 8px;
        text-decoration: none;
      }
      #O_o a:hover, #O_o a:active {
        color: #fff;
        border-bottom: #fff;
        text-decoration: none;
      }
      @media only screen and (max-width: 620px), only screen and (max-device-width: 620px) {
        #wrapper {
          width: 90%;
        }
        legend {
          font-size: 24px;
          font-weight: 500;
        }
      }
      </style>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>

    <script type="text/javascript">    
    </script>
    <script type="text/javascript" src="<?php echo base_url('scripts/js/validator.min.js'); ?>"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script> <!-- only added as a smoke test for js conflicts -->
</head>

<body>    
    <div id="wrapper">

      <form data-toggle="validator" role="form" id="form" name="extra" value="me">

        <div id="entry1" class="clonedInput">
          <h2 id="reference" name="reference" class="heading-reference">Entry #1</h2>
          <fieldset>

        <!-- Select Basic -->
        <label class="label_ttl control-label" for="title">Title:</label>


        <!-- Text input-->
        <div class="form-group">
          <label class="label_fn control-label" for="first_name">First name:</label>
          <input id="first_name" name="first_name" type="text" placeholder="" class="form-control" required>
          <p class="help-block">This field is required.</p>
        </div>


        </div><!-- end #entry1 -->


        <!-- Button -->
        <p>
          <a href="" onclick="myFunction()">Submit</a>
        </p>

        </fieldset>
        </form>
    </div> <!-- end wrapper -->

    <script>
    function myFunction(){
      $('#form').validator().on('submit', function (e) {
      if (e.isDefaultPrevented()) {
        // handle the invalid form...
      } else {
        // everything looks good!
      }
    })
    }
    </script>
</body>
</html>

我使用这个库进行验证 http://1000hz.github.io/bootstrap-validator/#validator-usage

我需要“onclick”来启动 JS 函数并运行验证,这应该给我一个 true(即通过)或 false(即失败)。

注意:我是 JS 方面的菜鸟,我本质上是在尝试拼凑它,以便它对我有用。

最佳答案

您可以在单击链接时调用函数,例如:

function myFunction(){
    $("#form").submit(function() {
        $(this).validator(function(e) {
            if (e.isDefaultPrevented()) {
                // handle the invalid form...
            } else {
                // everything looks good!
            }
        });
    });
}

来源:https://api.jquery.com/submit/

关于javascript - 使用 Javascript 进行 Bootstrap 表单验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30518153/

相关文章:

html - Bootstrap 网格不堆叠

angularjs - 如何为较大的表单部分禁用 ng-required 验证

c# - 获取作为字符串传入的属性的值

android - 如何在 Android 中使用 MVVM 数据绑定(bind)验证表单 ViewModel?

javascript - JQuery 图像 slider 使用 "Cycle Plugin": Why Won't My Images "Cycle"?

javascript - 如何使用 Jasmine 为 $resource (AngularJS) 定义测试

javascript - 通过点击事件选择文件夹名称

javascript - 无法使用 ReactJS 读取未定义的属性 'filter'

javascript - MVC Bootstrap Navbar - 事件类只保留一个 <li> 元素

javascript - Bootstrap 模态仅显示背景