jquery表单向导验证

时间:2010-01-12 04:48:02

标签: jquery validation formwizard

我正在尝试使用jquery表单向导(http://www.jankoatwarpspeed.com/post/2009/09/28/webform-wizard-jquery.aspx)实现验证脚本(bassistance),但我遇到了一些问题。

在jquery向导的页面上,一个名为“Tommy”的人想出了一段代码来实现代码的低音。但由于某种原因,我无法让它发挥作用。它出现说如果字段需要填写等,下一个按钮不起作用 - 这很好,但是,如果我填写所有字段,下一个按钮仍然不起作用..

function createNextButton(i) {

            var stepName = "step" + i;
            $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Next' class='next'>Next ></a>");

            /* VALIDATION */
            if (options.validationEnabled) {
                var stepIsValid = true; 
                $("#" + stepName + " :input").each(function(index) { 
                    stepIsValid = !element.validate().element($(this)) && stepIsValid;
                });
                if (!stepIsValid) {
                    return false; 
                }
            }
            /* END VALIDATION */

            $("#" + stepName + "Next").bind("click", function(e) {
                $("#" + stepName).hide();
                $("#step" + (i + 1)).show();
                if (i + 2 == count)
                    $(submmitButtonName).show();
                selectStep(i + 1);
            });

        }

有人可以帮我解决这个问题吗? :)

2 个答案:

答案 0 :(得分:1)

尝试将验证添加到点击事件

function createNextButton(i) {
    var stepName = "step" + i;
    $("#" + stepName + "commands").append("<a href='#' id='" + stepName + "Next' class='next'>Next ></a>");

    $("#" + stepName + "Next").bind("click", function(e) {
        /* VALIDATION */
        if (options.validationEnabled) {
            var stepIsValid = true; 
            $(this).closest("fieldset").find(":input").each(function(index) { 
                stepIsValid = element.validate().element($(this)) && stepIsValid;
            });
            if (!stepIsValid) {
                return false; 
            }
        }
        /* END VALIDATION */

        $("#" + stepName).hide();
        $("#step" + (i + 1)).show();
        if (i + 2 == count)
            $(submmitButtonName).show();
        selectStep(i + 1);
    });
}

<强>更新

或者...尝试从接收简报?复选框中删除默认检查(默认情况下取消选中)。 click事件未附加到下一个按钮,因为复选框是必需的并且已选中,因此stepIsValid为false,createNextButton在绑定click事件之前返回false。

答案 1 :(得分:1)

好的所以我在click事件中添加了验证,我发现“element.validate()。element($(this))&amp;&amp; stepIsValid”实际存在..如果有其他人正在使用这个并且有同样的问题,解决方案是:

/* VALIDATION */
                if (options.validationEnabled) {
                    var stepIsValid = true;
                    $("#"+stepName+" :input").each(function(index) {
                        checkMe = element.validate().element($(this));
                        //stepIsValid = !element.validate().element($(this)) && stepIsValid;
                        stepIsValid = checkMe && stepIsValid;
                    });
                    //alert("stepIsValid === "+stepIsValid);
                    if (!stepIsValid) {
                        return false;
                    };
                }; 
                /* END VALIDATION */
相关问题