jQuery表单验证不适用于引导程序模式

我正在使用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>

相同的代码在其他地方也可以正常工作。我的代码有什么问题?为什么会出现这种情况呢?

huhaohuhao7654 回答:jQuery表单验证不适用于引导程序模式

问题是“ DataTable当前不支持<tbody>标签中的colspan或rowpan”

我知道问题出在“ jquery.dataTables.min.js:24 Uncaught TypeError:Cannot set property'_DT_CellIndex'of undefined”的错误,这是因为列表页面上的jQuery数据表存在包含了引导模态文件。因此,我已通过以下方法解决了该问题

您可以使用隐藏的列而不是使用复杂的标题示例,将隐藏的列添加到tr的末尾,但它很笨拙,但我认为它更简单,更短:

<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

得到了答案 ,

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

onsubmit="event.preventDefault()"

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

正如我看到的代码所示,规则中还有一个逗号,如下所示。

teacher_id: {
   required: true,}

可能是您的表单将以手动方式提交,然后在控制台中显示JQuery Validation错误。请添加以下代码作为属性,然后尝试再次提交。

onsubmit="event.preventDefault()"

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

本文链接:https://www.f2er.com/3167352.html

大家都在问