jQuery - SmartWizard。如何覆盖“上一页”按钮验证?

时间:2011-09-06 08:24:30

标签: jquery client-side-validation smart-wizard

基本上,我需要在单击“下一步”按钮时验证数据,但如果用户想要返回,我不想验证他们是否已为当前步骤引入了必需的字段

    $('#wizard').smartWizard({ onLeaveStep: leaveAStepCallback,
            onFinish: onFinishCallback
        });

        function leaveAStepCallback(obj) {
            var step_num = obj.attr('rel'); // get the current step number
            return validateSteps(step_num); // return false to stay on step and true to continue navigation 
        }

        function onFinishCallback() {
            if (validateAllSteps()) {
                $('form').submit();
            }
        }

        // Your Step validation logic
        function validateSteps(stepnumber) {
            var isStepValid = true;

            if (stepnumber == 1) {
                var e = document.getElementById("ContentPlaceHolder1_DropDownCustomers");
                var strCustomer = e.options[e.selectedIndex].value;
                if (strCustomer == "-1") {
                    //alert("Please select a Customer.");
                    $('#wizard').smartWizard('showMessage', 'Please select a Customer.');
                    isStepValid = false;
                    return isStepValid;
                }
                else {
                    var d = document.getElementById("ContentPlaceHolder1_DropDownTemplates");
                    var strTemplate = d.options[d.selectedIndex].value;
                    if (strTemplate == "-1") {
                        alert("Please select a Template.");
                        isStepValid = false;
                        return isStepValid;
                    }
                    else {
                        return isStepValid;
                    }
                    return isStepValid;
                }
            }

            if (stepnumber == 2) {

                if (document.getElementById("ContentPlaceHolder1_LabelMainDestData") != null) {
                    isStepValid = true;
                }
                else {
                    alert("Please introduce the Main Destination.");
                    isStepValid = false;
                }


                return isStepValid;

            }

            if (stepnumber == 3) {
                isStepValid = true;
                return isStepValid;
            }

        }

6 个答案:

答案 0 :(得分:3)

showStep函数中的

只需编辑

if(stepIdx != curStepIdx)

if(stepIdx > curStepIdx)

它将解决您的问题

答案 1 :(得分:1)

创建一个新的var:

var OO_Back = false;

然后在点击后退按钮时添加“OO_Back = true”:

$($this.buttons.previous).click(function() {
    OO_Back = true;
    $this.goBackward();
    return false;
});

最后修改此条件以检查是否单击了后退按钮,之后必须设置OO_Back = false:

 var curStep = $this.steps.eq($this.curStepIdx);
    if(stepIdx != $this.curStepIdx){
        if($.isFunction($this.options.onLeaveStep) && OO_Back == false) {
            var context = { fromStep: $this.curStepIdx+1, toStep: stepIdx+1 };
            if (! $this.options.onLeaveStep.call($this,$(curStep), context)){
                return false;
            }
        }
    }
 OO_Back = false;

答案 2 :(得分:0)

只需在step_number属性上创建另一个jsp页面,您可以从请求中获取该内容,jsp根据您的step_number包含您的jsp,并在您的contentURL中分配新创建的jsp

实施例

//inside your wizard page.
contentURL:'wizardcontent.jsp',

//'wizardcontent.jsp' is as follow

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%
    int step_number = Integer.parseInt(request.getParameter("step_number")!=null?request.getParameter("step_number"):"1");
    String url = "";
    switch (step_number){
        case 1:%>
            <jsp:include page="ddd.jsp"></jsp:include>
        <%break;
        case 2:%>
            <jsp:include page="xyz.jsp"></jsp:include>
        <%break;
        case 3:%>
            <jsp:include page="abcd.jsp"></jsp:include>
                   <%break;
    }

%>

答案 3 :(得分:0)

也许这会有所帮助:

       $('.buttonPrevious').click(function () {
            // set true validation if you need
            return false;
        });

如果需要,您必须在后退按钮上设置有效的true。

尝试使用取消的leaveAStepCallback函数。

       $('#yourwizard').unbind('leaveAStepCallback'); 

如果无效,请查看stopImmediatePropagation

答案 4 :(得分:0)

我使用了以下内容,它完美地运用了上面接受的答案。

onLeaveStep: function(obj, context){
       if(context.toStep > context.fromStep){ ...}
  }

答案 5 :(得分:0)

当我使用它时,这对我有用:

 function leaveStep(oStep, context) {
   //don't validate if the user is going backwards    
    if (context.toStep < context.fromStep)
    {
        $('#wizard').smartWizard('hideMessage');
        return true;
    }
   // continue to validate steps...