简体   繁体   English

如何在jquery中验证包含引导选项卡的动态生成表单

[英]how to validate dynamically generated form containing bootstrap tabs in jquery

In my project i'm dynamically generating an html form using jquery. 在我的项目中,我使用jquery动态生成一个html表单。 Here the page consist of five tabs each having separate previous and next buttons. 这里的页面包含五个选项卡,每个选项卡都有单独的上一个和下一个 The tab is designed using bootstrap. 该选项卡使用bootstrap设计。 Can anyone please suggest the easiest method to validate this page. 任何人都可以建议最简单的方法来验证此页面。 ie, when i click next button on first tab it should validate fields in first tab only and so on. 即,当我单击第一个选项卡上的下一个按钮时,它应仅在第一个选项卡中验证字段,依此类推。 Please help me to solve this issue. 请帮我解决这个问题。 Many thanks in advance. 提前谢谢了。

Here is my whole code: 这是我的整个代码:

html code: HTML代码:

<ul class="nav nav-tabs">
    <li class="active"><a data-toggle="tab" href="#name" id="id-name">name</a></li>
    <li><a data-toggle="tab" href="#address" id="id-address">address</a></li>
</ul>

 <form class="form-horizontal" id="id-fn-employee" method="POST"  enctype="multipart/form-data" action="${pageContext.request.contextPath}/employee/saveMe">
    <div class="tab-content">
        <div id="name" class="tab-pane fade in active" style="height:auto;overflow: hidden;">
        <span id ="name1"></span>
           <div class="form-group">
                <label class="col-sm-3 control-label">First Name<a style="color: red">*</a>
                    <p style="font-size: 75%;" class="help-block">First Name</p>
                </label>
                <div class="col-xs-4">
                    <input type="text" placeholder="First Name" required="true" id="id-firstNmae" name="firstNmae" class="form-control">
                    <p style="font-size: 75%;" class="help-block"></p>
                </div>
           </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">Last Name<a style="color: red">*</a>
                    <p style="font-size: 75%;" class="help-block">Last Name</p>
                </label>
                <div class="col-xs-4">
                    <input type="text" placeholder="Last Name" required="true" id="id-lastName" name="lastName" class="form-control">
                    <p style="font-size: 75%;" class="help-block"></p>
                </div>
           </div>
           <ul class="pager">
                <li><a href="javascript:void(0);" id="id-cancel">Cancel</a></li>
                <li><a href="javascript:showTab('address');" id="id-name-next">Next</a></li>
            </ul>
        </div>
        <div id="address" class="tab-pane fade" style="height:auto;overflow: hidden;">
        <span id ="address1"></span>
            <div class="form-group">
                <label class="col-sm-3 control-label">house name<a style="color: red">*</a>
                    <p style="font-size: 75%;" class="help-block">house name</p>
                </label>
                <div class="col-xs-4">
                    <input type="text" placeholder="house name" required="true" id="id-houseName" name="houseName" class="form-control">
                    <p style="font-size: 75%;" class="help-block"></p>
                </div>
           </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">Street<a style="color: red">*</a>
                    <p style="font-size: 75%;" class="help-block">Street</p>
                </label>
                <div class="col-xs-4">
                    <input type="text" placeholder="Street" required="true" id="id-street" name="street" class="form-control">
                    <p style="font-size: 75%;" class="help-block"></p>
                </div>
           </div>
            <ul class="pager">
                <li><a href="javascript:showPrevTab('name');" id="id-address-prev">Previous</a></li>
                <li><a href="javascript:showTab('age');" id="id-age-next">Next</a></li>
            </ul>
        </div>
    </div>
</form>

JS: JS:

$(document).ready(function(){
      var $validator = $("#id-fn-employee").validate({
        errorPlacement: function (error, element) {
            var placement = $(element).data('error');
            if( $(element).hasClass('grp') ){
                error.insertAfter($(element).parent());
            }else{
                if (placement) {
                    $(placement).append(error)
                } else {
                    error.insertAfter(element);
                }
            }
        }
    });
});

I have included the required js files along with 我已经包含了所需的js文件

<script src="${pageContext.request.contextPath}/resources/js/plugins/jquery.validate.min.js"></script>

Here we need to add the above code in js and need to specify the id of the form. 这里我们需要在js中添加上面的代码,并且需要指定表单的id。 This validation is working fine in static pages but failed to validate dynamically generated form. 此验证在静态页面中正常工作,但无法验证动态生成的表单。

Let's assume you have some markup like the following for simplicity: 为简单起见,我们假设您有一些类似下面的标记:

<ul class="tabs">
   <li class="tab" id="first_tab">
       <form>
          /* form fields are here */
          <button class="next_btn">Next</button>
       </form>
   </li>
</ul>

So you can listen to the click event on the Next button: 因此,您可以在“ 下一步”按钮上收听click事件:

$('form .next_btn').on('click', function () {
    var $btn = $(this),
        $form = $btn.closest('form');

    initValidator($form);

    if($form.valid()){
       // Go to next step
    } else {
       alert('Invalid form')
    }
});


function initValidator($form) {
    var $validator = $form.validate({
        errorPlacement: function (error, element) {
            var placement = $(element).data('error');
            if( $(element).hasClass('grp') ){
                error.insertAfter($(element).parent());
            }else{
                if (placement) {
                    $(placement).append(error)
                } else {
                    error.insertAfter(element);
                }
            }
        }
    });
}

Problem : This looks messy, Being this your current structure. 问题:这看起来很混乱,这就是你目前的结构。

<form class="form-horizontal" id="id-fn-employee" method="POST"  enctype="multipart/form-data" action="${pageContext.request.contextPath}/employee/saveMe">
  <div class="tab-content">
    <div id="name">...</div>
    <div id="address">...</div>
  </div>
</form>

And your script 和你的脚本

var $validator = $("#id-fn-employee").validate({...

Since your have the form as a parent which wraps both the tab content, at any given tab you will end up in validating the entire form. 由于您将form作为父级包装选项卡内容,因此在任何给定的选项卡中,您最终将验证整个表单。


Solution: 解:

1) Have a seperate forms for each tab, and on click of next button you can validate only that respective tab's form. 1)每个选项卡都有一个单独的表单,单击下一个按钮,您只能验证相应选项卡的表单。 So your Html should be restructured this way. 所以你的Html应该以这种方式重组。

  <div class="tab-content">
    <div id="name"><form>...</form></div>
    <div id="address"><form>...</form></div>
  </div>

And when both forms are valid you can combine the data of both the forms and then submit using Jquery. 当两个表单都有效时,您可以组合两个表单的数据,然后使用Jquery提交。

2) If you dont want to restructure your HTML then you have to manually grab only the elements in that particular tab content div and then validate them manually. 2)如果您不想重构HTML,则必须手动仅抓取该特定选项卡内容div中的元素,然后手动验证它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM