【问题标题】:jQuery Form Validation is not working on bootstrap modaljQuery 表单验证不适用于引导模式
【发布时间】:2019-11-04 10:57:08
【问题描述】:

我正在使用 this 的 jQuery 验证 我正在引导程序模式上编写表单验证,但是当我点击提交按钮时它无法正常工作,它被提交而没有显示表单验证错误。我的代码如下:

$(document).ready(function () {

    $('#add_section_form').validate({
      rules: {
        section: {
          required: true
        },
        category: {
          required: true
        },
        capacity: {
          required: true
        },
        class_of_section: {
          required: true
        },
        teacher_id: {
          required: true,
        }
      },
      messages: {
        section: {
          required: "The Section field is required"
        },
        category: {
          required: "The category field is required"
        },
        capacity: {
          required: "The capacity field is required"
        },
        class_of_section: {
          required: "The class field is required"
        },
        teacher_id: {
          required: "The teacher name field is required"
        }
      },
      submitHandler: function (form) { // for demo
        // alert('valid form submitted');
        // return false;
        form.submit();
      }
    });
  });

我的带有表单的引导模式 - add-section-modal.php

 <!-- Modal -->
<div id="addSectionModal" class="modal fade" role="dialog">
 <div class="modal-dialog modal-lg">

 <!-- Modal content-->
 <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times; 
       </button>
      <h4 class="modal-title text-center">ADD SECTION</h4>
    </div>
    <form id="add_section_form" data-parsley-validate class="form-horizontal form-label-left" action="<?php echo base_url();?>sections/save" method="post" enctype="multipart/form-data" >
    <div class="modal-body">
      <div class="container">
        <div class="row">
         <div class='col-md-4 col-sm-12 col-xs-12 form-group'>
           Section *
          <input type="text" id="section" name='section' class="form-control">
        </div>
        <div class='col-md-4 col-sm-12 col-xs-12 form-group'>
         Category *
         <input type="text" id="category" name="category" class="form-control">
        </div>
        <div class='col-md-4 col-sm-12 col-xs-12 form-group'>
         Capacity *
         <input type="text" id="capacity" name="capacity" class="form-control">
        </div>
        <div class='col-md-4 col-sm-12 col-xs-12 form-group'>
         Class
         <select id="class_of_section" class="form-control" name="class_of_section">
          <option value="">Choose..</option>
          <option value="class1" >Class1</option>
          <option value="class2" >Class2</option>
         </select>
        </div>
        <div class='col-md-4 col-sm-12 col-xs-12 form-group'>
         Teacher Name
          <select id="teacher_id" class="form-control" name="teacher_id">
             <option value="">Choose..</option>
             <option value="teacher1" >Teacher1</option>
             <option value="teacher2" >Teacher2</option>
          </select>
        </div>
        <div class='col-md-4 col-sm-12 col-xs-12 form-group'>
         Note
         <textarea class="form-control" name="notes"></textarea>
        </div>
       </div>
      </div>
     </div>
     <div class="modal-footer">
      <input type="submit" class="btn btn-success" value="Submit">
       <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    </div>
    </form>
    </div>
    </div>
   </div>

我已将此模式代码存储在单独的文件中,并且我已将该文件包含在列表页面中,并且还将 jQuery 验证脚本包含在列表页面本身中,并且我正在使用列表页面上的添加按钮打开添加模式形式。如下:

list.php

-----------
  $this->load->view('add-section-modal.php');
-----------
<script>
-----------------
 My above jQuery Validation script goes here
------------------
</script>

相同的代码在其他地方运行良好。我的代码有什么问题?为什么,在这种情况下,它不能正常工作任何帮助。

【问题讨论】:

  • 当 html 被转换为模式时,bootstrap 会从 HTML 中删除它并重新添加它,因此在此之前连接的所有事件都会丢失。这可能会在您的代码之后发生。显示模态时需要重新初始化验证。
  • 我也试过了。请再次检查我的问题,因为我已再次更新。
  • 您在上面发布的代码运行良好。 jsfiddle.net/vwud83s5/1 ~ 如果您只向我们展示有效的代码,我们将无能为力。您是否进行了任何故障排除?检查控制台是否有 JavaScript 错误?检查实时 DOM 以确保它看起来像上面那样?检查 DOM 以确保正确包含所有相关文件?有任何故障排除吗?
  • 我已经检查了控制台中的错误。将一个与数据表错误相关的 javascript 显示为“jquery.dataTables.min.js:24 Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined”
  • 您意识到一个 JavaScript 错误会阻止 JavaScript 的其余部分运行。

标签: javascript jquery jquery-validate


【解决方案1】:

问题是“DataTables目前不支持&lt;tbody&gt;标签中的colspan或rowspan”

我知道问题是错误“jquery.dataTables.min.js:24 Uncaught TypeError: Cannot set property '_DT_CellIndex' of undefined”,这是因为我拥有的列表页面上的 jQuery 数据表包括引导模式文件。所以我已经通过以下方式解决了这个问题

您可以将隐藏列添加到 tr 的末尾,而不是使用复杂的标题示例,这在我看来很 hacky 但更简单更短:

<thead>
<tr>    
  <th>&nbsp;</th> <!-- column 1 -->
  <th colspan="2">&nbsp;</th> <!-- column 2&3 -->
  <th colspan="2">&nbsp;</th> <!-- column 4&5 -->

  <!-- hidden column 6 for proper DataTable applying -->
  <th style="display: none"></th> 
</tr>
</thead>

<tbody>
<tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>

  <!-- hidden column 6 for proper DataTable applying -->
  <td style="display: none"></td>
</tr>
</tbody>  

我从'renkse'here得到了答案

【讨论】:

    【解决方案2】:

    以前我在尝试使用 JQuery Validation 时遇到了同样的问题。表单没有得到验证并手动提交,没有显示任何错误。然后,当我在提交表单时打开开发人员控制台时,在单击提交按钮时打印了一些错误,同时由于表单提交而刷新了页面。所以,我意识到我的脚本中有一些错误,但由于表单提交时的页面加载,我无法阅读它。所以我通过添加属性手动停止表单提交

    onsubmit="event.preventDefault()"
    

    然后当我单击提交按钮时表单提交停止并且它没有重新加载页面。现在我可以读取开发人员控制台中显示的错误。我遇到的大多数错误仅是尾随逗号和额外的大括号。因此,请尝试以下解决方案,因为它对我有用。

    当我看到你的代码时,规则中有一个额外的逗号,如下所示。

    teacher_id: {
       required: true,
    }
    

    在控制台中显示 JQuery 验证错误之前,您的表单可能会手动提交。请添加以下代码作为属性并尝试再次提交。

    onsubmit="event.preventDefault()"
    

    现在它会停止你的表单提交,然后如果你在开发者控制台中看到,你会发现 JQuery 验证错误。

    【讨论】:

    • 我已经尝试过了,但没有运气,提交表单的结果相同,但没有显示错误。
    • 尾随逗号过去只是在 Explorer 版本 6 或类似版本的恐龙时代出现的问题。此外,使用此插件时,您绝对不需要在任何地方添加preventDefault()。请不要发布猜测作为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    相关资源
    最近更新 更多