【问题标题】:Jquery validate always return truejQuery 验证总是返回 true
【发布时间】:2016-06-20 06:10:45
【问题描述】:

我有一个 jquery 步骤向导,其中存在两种形式。当传递到下一步时,我尝试使用 valid() 方法验证第一个表单,但 jquery validate 始终返回 true。

这是小提琴:

https://jsfiddle.net/2uyp1g25/

$(document).ready(function(){
   var formBasico = $("#basico").show();
    formBasico.validate({
        ignore: ":disabled,:hidden",
        rules: {
            'nombre-temp': {
                required: true,
                range: [1, 50]
            },
            'descripcion-temp': {
                required: true,
                range: [1, 50]
            },
            'min-cli': {
                required: true,
                number: true
            },
            'max-cli': {
                required: true,
                number: true
            },
            'cantidad-celdas': {
                required: true,
                number: true
            }
        }
    });

   $('#pasosTemplate').steps({
        headerTag: "h3",
        bodyTag: "section",
        transitionEffect: "slideLeft",
        autoFocus: true,
        labels: {
            cancel: "Cancelar",
            current: "paso actual:",
            pagination: "Paginación",
            finish: "Guardar Template",
            next: "Siguiente",
            previous: "Anterior",
            loading: "Cargando ..."
        },
        onStepChanging: function (event, currentIndex, newIndex) {
            if (currentIndex < newIndex) {
                alert(formBasico.valid());
                return formBasico.valid();
            }
            return true;
        },
        onFinished: function (event, currentIndex) {
         alert('fin');
        }
    });
});
/*
    Common
*/

.wizard,
.tabcontrol
{
    display: block;
    width: 100%;
    overflow: hidden;
}

.wizard a,
.tabcontrol a
{
    outline: 0;
}

.wizard ul,
.tabcontrol ul
{
    list-style: none !important;
    padding: 0;
    margin: 0;
}

.wizard ul > li,
.tabcontrol ul > li
{
    display: block;
    padding: 0;
}

/* Accessibility */
.wizard > .steps .current-info,
.tabcontrol > .steps .current-info
{
    position: absolute;
    left: -999em;
}

.wizard > .content > .title,
.tabcontrol > .content > .title
{
    position: absolute;
    left: -999em;
}



/*
    Wizard
*/

.wizard > .steps
{
    position: relative;
    display: block;
    width: 100%;
}

.wizard.vertical > .steps
{
    display: inline;
    float: left;
    width: 30%;
}

.wizard > .steps .number
{
    font-size: 1.429em;
}

.wizard > .steps > ul > li
{
    width: 25%;
}

.wizard > .steps > ul > li,
.wizard > .actions > ul > li
{
    float: left;
}

.wizard.vertical > .steps > ul > li
{
    float: none;
    width: 100%;
}

.wizard > .steps a,
.wizard > .steps a:hover,
.wizard > .steps a:active
{
    display: block;
    width: auto;
    margin: 0 0.5em 0.5em;
    padding: 1em 1em;
    text-decoration: none;

    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
}

.wizard > .steps .disabled a,
.wizard > .steps .disabled a:hover,
.wizard > .steps .disabled a:active
{
    background: #eee;
    color: #aaa;
    cursor: default;
}

.wizard > .steps .current a,
.wizard > .steps .current a:hover,
.wizard > .steps .current a:active
{
    background: #007cc1;
    color: #fff;
    cursor: default;
}

.wizard > .steps .done a,
.wizard > .steps .done a:hover,
.wizard > .steps .done a:active
{
    background: #9dc8e2;
    color: #fff;
}

.wizard > .steps .error a,
.wizard > .steps .error a:hover,
.wizard > .steps .error a:active
{
    background: #ff3111;
    color: #fff;
}

.wizard > .content
{
    background: #eee;
    display: block;
    margin: 0.5em;
    min-height: 35em;
    overflow: hidden;
    position: relative;
    width: auto;

    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
}

.wizard.vertical > .content
{
    display: inline;
    float: left;
    margin: 0 2.5% 0.5em 2.5%;
    width: 65%;
}

.wizard > .content > .body
{
    float: left;
    position: relative;
    width: 95%;
    height: 95%;
    padding: 2.5%;
}

.wizard > .content > .body ul
{
    list-style: disc !important;
}

.wizard > .content > .body ul > li
{
    display: list-item;
}

.wizard > .content > .body > iframe
{
    border: 0 none;
    width: 100%;
    height: 100%;
}

.wizard > .content > .body input
{
    display: block;
    border: 1px solid #ccc;
}

.wizard > .content > .body input[type="checkbox"]
{
    display: inline-block;
}

.wizard > .content > .body input.error
{
    background: rgb(251, 227, 228);
    border: 1px solid #fbc2c4;
    color: #8a1f11;
}

.wizard > .content > .body label
{
    display: inline-block;
    margin-bottom: 0.5em;
}

.wizard > .content > .body label.error
{
    color: #8a1f11;
    display: inline-block;
    margin-left: 1.5em;
}

.wizard > .actions
{
    position: relative;
    display: block;
    text-align: right;
    width: 100%;
}

.wizard.vertical > .actions
{
    display: inline;
    float: right;
    margin: 0 2.5%;
    width: 95%;
}

.wizard > .actions > ul
{
    display: inline-block;
    text-align: right;
}

.wizard > .actions > ul > li
{
    margin: 0 0.5em;
}

.wizard.vertical > .actions > ul > li
{
    margin: 0 0 0 1em;
}

.wizard > .actions a,
.wizard > .actions a:hover,
.wizard > .actions a:active
{
    background: #007CC1;
    color: #fff;
    display: block;
    padding: 0.5em 1em;
    text-decoration: none;

    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
}

.wizard > .actions .disabled a,
.wizard > .actions .disabled a:hover,
.wizard > .actions .disabled a:active
{
    background: #eee;
    color: #aaa;
}

.wizard > .loading
{
}

.wizard > .loading .spinner
{
}



/*
    Tabcontrol
*/

.tabcontrol > .steps
{
    position: relative;
    display: block;
    width: 100%;
}

.tabcontrol > .steps > ul
{
    position: relative;
    margin: 6px 0 0 0;
    top: 1px;
    z-index: 1;
}

.tabcontrol > .steps > ul > li
{
    float: left;
    margin: 5px 2px 0 0;
    padding: 1px;

    -webkit-border-top-left-radius: 5px;
    -webkit-border-top-right-radius: 5px;
    -moz-border-radius-topleft: 5px;
    -moz-border-radius-topright: 5px;
    border-top-left-radius: 5px;
    border-top-right-radius: 5px;
}

.tabcontrol > .steps > ul > li:hover
{
    background: #edecec;
    border: 1px solid #bbb;
    padding: 0;
}

.tabcontrol > .steps > ul > li.current
{
    background: #fff;
    border: 1px solid #bbb;
    border-bottom: 0 none;
    padding: 0 0 1px 0;
    margin-top: 0;
}

.tabcontrol > .steps > ul > li > a
{
    color: #5f5f5f;
    display: inline-block;
    border: 0 none;
    margin: 0;
    padding: 10px 30px;
    text-decoration: none;
}

.tabcontrol > .steps > ul > li > a:hover
{
    text-decoration: none;
}

.tabcontrol > .steps > ul > li.current > a
{
    padding: 15px 30px 10px 30px;
}

.tabcontrol > .content
{
    position: relative;
    display: inline-block;
    width: 100%;
    height: 35em;
    overflow: hidden;
    border-top: 1px solid #bbb;
    padding-top: 20px;
}

.tabcontrol > .content > .body
{
    float: left;
    position: absolute;
    width: 95%;
    height: 95%;
    padding: 2.5%;
}

.tabcontrol > .content > .body ul
{
    list-style: disc !important;
}

.tabcontrol > .content > .body ul > li
{
    display: list-item;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/jquery.validate.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-steps/1.1.0/jquery.steps.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="/css/result-light.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-steps/1.1.0/jquery.steps.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.15.0/jquery.validate.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css">
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/js/bootstrap.min.js"></script>



<body>

<div id="pasosTemplate">
                <h3>Información Básica</h3>
                <section>
                    <form class="form-horizontal" id="basico">
                        <div class="form-group">
                            <label class="col-lg-2">Nombre Template</label>
                            <div class="col-lg-6">
                                <input type="text" class="form-control" placeholder="Nombre Template" id="nombre-temp" name="nombre-temp" >
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-2">Descripción Template</label>
                            <div class="col-lg-6">
                                <input type="text" class="form-control" placeholder="Descripción Template" id="descripcion-temp" name="descripcion-temp">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-2">Objetivo de Optimización</label>
                            <div class="col-lg-6">
                                <select class="form-control" id="objetivo-o" name="objetivo-o">
                                    <option value=" ">Selecciona un objetivo...</option>
                   
                                </select>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-2">Establecer cantidad Mínima y Máxima de clientes</label>
                            <div class="col-lg-2">
                                <input type="number" class="form-control" placeholder="Mínimo" id="min-cli" name="min-cli">
                            </div>
                            <div class="col-lg-2">
                                <input type="number" class="form-control" placeholder="Máximo" id="max-cli" name="max-cli">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-lg-2">Celdas de Comunicación</label>
                            <div class="col-lg-2">
                                <input type="number" placeholder="Cantidad celdas" class="form-control" id="cantidad-celdas" name="cantidad-celdas">
                            </div>
                        </div>
                    </form>
                </section>
                <h3>Segmentación</h3>
                <section>
                    <form class="form-horizontal" id="segmentacion" action="#">
                  
                        <div class="col-lg-12" id="segmentaciones">

                        </div>
                        <div class="form-group">
                            <label class="col-lg-2">Estimación de Clientes alcanzados</label>
                            <div class="col-lg-4">
                                <input type="text" readonly class="form-control" id="clientes-alcanzados">
                            </div>
                        </div>
                    </form>
                </section>
            </div>
            </body>

如何解决这个错误???

【问题讨论】:

    标签: jquery jquery-validate jquery-steps


    【解决方案1】:

    您已将 formBasico 变量名称分配给包含 .show() 方法的内容。

    var formBasico = $("#basico").show();
    

    然后您将其附加到 .valid() 方法...

    alert(formBasico.valid());
    

    您只能将.valid() 方法附加到表单对象或表单的数据输入元素。但是,在您的情况下,formBasico 变量既不代表。

    通过仅将.valid() 附加到代表表单对象的内容来修复它...

    $("#basico").valid()
    

    查看您的 jsFiddle,我可以通过将 .valid() 方法移出 .steps() 来测试该方法,并验证表单。

    https://jsfiddle.net/2uyp1g25/1/

    由于某种原因,.valid() 在从 .steps() 方法中调用时停止工作。我想在.steps() 中返回true,因为步骤插件在调用.valid() 之前隐藏了面板。隐藏后,表单将有效,因为您忽略了隐藏字段。

    The documentation for StepsonStepChanging

    在步骤更改之前触发,并可用于通过返回false 来防止步骤更改。对表单验证非常有用。

    尽管您的演示证明并非如此。

    • 如果我return falseonStepChanging内,我可以停止这一步;但是我无法在return false 之前或之后运行任何其他代码。

    • 如果我按照你的方式尝试,在.valid() 可以运行之前步骤更改,然后.valid() 为真,因为该步骤是隐藏的。

    我认为这是 Steps 中的错误。

    当我尝试onFinishingwhich states...时,我得到了相同的结果...

    在完成之前触发,可用于通过返回 false 来阻止完成。对表单验证非常有用。

    我只是把return false 放在这里,它甚至不能像上面的文档中描述的那样工作!如果return false 甚至不会停止该步骤,那么您将无法在其中运行任何其他代码。

    https://jsfiddle.net/2uyp1g25/2/

    为什么有两个完全不同的选项,都被描述为“对表单验证非常有用”,其中一个正常工作就足够了?

    有带有 Steps 插件的 presently 109 open issuesissues within onStepChanging,其中许多描述了表单验证问题。自 2014 年 9 月以来,该插件似乎也没有更新或维护。


    编辑

    经过更多的实验,我发现可以通过将验证规则内联声明为类名而不是在 rules 对象内来“修复”这个问题...

    https://jsfiddle.net/2uyp1g25/3/

    无论你如何声明规则,jQuery Validate 的工作方式都是一样的,所以唯一可能的区别是它的时间。这只是确认 Steps 插件在其 onStepChanging 回调的工作方式方面存在一些严重问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多