UI选项卡,下一个上一个按钮,使用完整HTML查询验证

时间:2014-12-11 05:05:03

标签: javascript jquery

我正在使用jQuery验证进行UI选项卡,同时执行下一个和上一个按钮功能。一切正常,但验证不是。我该如何解决这个问题?

我在下面放了我的HTML和JavaScript代码。

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
</head>
<body>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.8.3/themes/base/jquery-ui.css" />
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://code.jquery.com/ui/1.8.3/jquery-ui.js"></script>
<script src="jquery.steps.js"></script>
<script src="jquery.validate.js"></script>
<script src="jquery.ui.core.js"></script>
<script src="jquery.ui.widget.js"></script>
<script src="tabs.js"></script>


<script type="text/javascript" src="jsfile1.js"></script>
<link rel="stylesheet" href="jquery.steps.css" />

<div id="tabs">
  <ul>
  <li><a href="#account">1. Account</a></li>
    <li><a href="#profile">2. Profile</a></li>
    <li><a href="#warning">3. Warning</a></li>
    <li><a href="#finish">4. Finish</a></li>
  </ul>
<form id="example-advanced-form" action="#" enctype="multipart/form-data">
  <div id="account">
    <h3>Account</h3>
    <fieldset>
    <legend>Account Information</legend>

    <label for="userName-2">User name *</label>
    <input id="userName-2" name="userName" type="text" class="required">
    <label for="password-2">Password *</label>
    <input id="password-2" name="password" type="text" class="required">
    <label for="confirm-2">Confirm Password *</label>
    <input id="confirm-2" name="confirm" type="text" class="required">
    <p>(*) Mandatory</p>
    </fieldset>


  </div>
  <div id="profile">
    <h3>Profile</h3>
    <fieldset>
    <legend>Profile Information</legend>

    <label for="name-2">First name *</label>
    <input id="name-2" name="name" type="text" class="required">
    <label for="surname-2">Last name *</label>
    <input id="surname-2" name="surname" type="text" class="required">
    <label for="email-2">Email *</label>
    <input id="email-2" name="email" type="text" class="required email">
    <label for="address-2">Address</label>
    <input id="address-2" name="address" type="text">
    <label for="age-2">Age (The warning step will show up if age is less than 18) *</label>
    <input id="age-2" name="age" type="text" class="required number">
    <p>(*) Mandatory</p>
    </fieldset>
  </div>
  <div id="warning">
    <h3>Warning</h3>
    <fieldset>
    <legend>You are to young</legend>

    <p>Please go away ;-)</p>
    </fieldset>
  </div>
  <div id="finish">
    <h3>Finish</h3>
    <fieldset>
    <legend>Terms and Conditions</legend>

    <input id="acceptTerms-2" name="acceptTerms" type="checkbox" class="required"> <label for="acceptTerms-2">I agree with the Terms and Conditions.</label>
    </fieldset>
  </div>

</form>
</div>
<input type="button" id="btnPrevious" value="Previous" style = "display:none"/>
<input type="button" id="btnNext" value="Next" /> 
    </body>
</html>

JavaScript的:

    $(document).ready(function() {
    alert("1")  
    var form = $("#example-advanced-form").show();

    form.steps({

    headerTag: "h3",
    bodyTag: "fieldset",
    transitionEffect: "slideLeft",
    onStepChanging: function (event, currentIndex, newIndex)
    {
    alert("2")
    // Always allow previous action even if the current form is not valid!
    if (currentIndex > newIndex)
    {
    return true;
    }
    // Forbid next action on "Warning" step if the user is to young
    if (newIndex === 3 && Number($("#age-2").val()) < 18)
    {
    return false;
    }
    // Needed in some cases if the user went back (clean up)
    if (currentIndex < newIndex)
    {
    // To remove error styles
    form.find(".body:eq(" + newIndex + ") label.error").remove();
    form.find(".body:eq(" + newIndex + ") .error").removeClass("error");
    }
    form.validate().settings.ignore = ":disabled,:hidden";
    return form.valid();
    },
    onStepChanged: function (event, currentIndex, priorIndex)
    {
    // Used to skip the "Warning" step if the user is old enough.
    if (currentIndex === 2 && Number($("#age-2").val()) >= 18)
    {
    form.steps("Next");
    }
    // Used to skip the "Warning" step if the user is old enough and wants to the previous step.
    if (currentIndex === 2 && priorIndex === 3)
    {
    form.steps("Previous");
    }
    },
    onFinishing: function (event, currentIndex)
    {
    form.validate().settings.ignore = ":disabled";
    return form.valid();
    },
    onFinished: function (event, currentIndex)
    {
    alert("Submitted!");
    }
    }).validate({
    errorPlacement: function errorPlacement(error, element) { element.before(error); },
    rules: {
    confirm: {
    equalTo: "#password-2"
    }
    }
    });


 var currentTab = 0;
    $(function () {
        $("#tabs").tabs({
            select: function (e, i) {
                currentTab = i.index;
            }
        });
    });
    $("#btnNext").live("click", function () {

        var tabs = $('#tabs').tabs();
        var c = $('#tabs').tabs("length");
        currentTab = currentTab == (c - 1) ? currentTab : (currentTab + 1);
        tabs.tabs('select', currentTab);
        $("#btnPrevious").show();
        if (currentTab == (c - 1)) {
            $("#btnNext").hide();
        } else {
            $("#btnNext").show();
        }
    });
    $("#btnPrevious").live("click", function () {
        var tabs = $('#tabs').tabs();
        var c = $('#tabs').tabs("length");
        currentTab = currentTab == 0 ? currentTab : (currentTab - 1);
        tabs.tabs('select', currentTab);
        if (currentTab == 0) {
            $("#btnNext").show();
            $("#btnPrevious").hide();
        }
        if (currentTab < (c - 1)) {
            $("#btnNext").show();
        }
    });
  });   

0 个答案:

没有答案