1

我创建了以下表格:

http://jsfiddle.net/baumdexterous/KKqbT/1/

    <!-- VALIDATION NOTIFICATION -->
    <div id="drawer">Please fill in the empty fields marked with  a <samp style="color:red">red</samp> border.</div>
    <!-- END VALIDATION NOTIFICATION -->


    <!-- FORM -->
    <form action="#">
      <div id="wizard">
        <div class="items">
          <div class="page">
              <ul>
                <li class="required">

                <!-- INPUT -->
                <label>Your Email:<br><input type="text" class="text" name="email"></label>

                <!-- SELECT DROPDOWN -->
                <label>Your City
                    <select name="city">
                        <option value="">-- please select --</option>
                        <option>Helsinki</option>
                        <option>Berlin</option>
                        <option>New York</option>
                    </select>
                </label>

                <!-- RADIO -->
                <label>What's the U.S. capital?<br>
                    <div class="qselections">
                        <input type="radio" value="a" name="question1">a) New York<br>
                        <input type="radio" value="b" name="question1">b) Washington DC<br>
                        <input type="radio" value="c" name="question1">c) Seattle<br>
                        <input type="radio" value="d" name="question1">d) Portland<br>
                    </div>
                </label>            
          </li>

          <li class="clearfix">
            <button type="button" class="next right">
                Proceed »
            </button>
          </li>

        </ul>

          </div> <!-- end page -->


        </div><!--items-->

      </div><!--wizard-->

    </form>

这是现有的验证:

    $(function() {
      var root = $("#wizard").scrollable();

      // some variables that we need
    var api = root.scrollable(), drawer = $("#drawer");

    // validation logic is done inside the onBeforeSeek callback
    api.onBeforeSeek(function(event, i) {

    // we are going 1 step backwards so no need for validation
    if (api.getIndex() < i) {

                 // 1. get current page
                 var page = root.find(".page").eq(api.getIndex()),

             // 2. .. and all required fields inside the page
             inputs = page.find(".required :input").removeClass("error"),

             // 3. .. which are empty
             empty = inputs.filter(function() {
             return $(this).val().replace(/\s*/g, '') == '';
             });

                 // if there are empty fields, then
                 if (empty.length) {

             // slide down the drawer
             drawer.slideDown(function()  {

             // colored flash effect
             drawer.css("backgroundColor", "#229");
             setTimeout(function() { drawer.css("backgroundColor", "#fff"); }, 1000);
             });

             // add a CSS class name "error" for empty & required fields
             empty.addClass("error");

             // cancel seeking of the scrollable by returning false
             return false;

                 // everything is good
                 } else {

             // hide the drawer
             drawer.slideUp();
                 }

                     }

                     // update status bar
                     $("#status li").removeClass("active").eq(i).addClass("active");

                         });

                             // if tab is pressed on the next button seek to next page
    root.find("button.next").keydown(function(e) {
    if (e.keyCode == 9) {

    // seeks to next tab by executing our validation routine
    api.next();
    e.preventDefault();
    }
    });
                           });

我能够验证输入部分并选择下拉表单元素就好了......但由于某种原因无法验证单选表单元素。任何人都知道我需要专门对这段代码做什么才能使无线电验证工作?谢谢你。

4

2 回答 2

2

工作演示 http://jsfiddle.net/7qD6F/ http://jsfiddle.net/4Shvw/ http://jsfiddle.net/7N8K9/

无警报版本 http://jsfiddle.net/8tMbV/

希望这能满足您的需要:)

我已经input单独处理了单选按钮,以便让您清楚!

代码

  $(function () {
      var root = $("#wizard").scrollable();
      var isRadioCheck = false;  //<======================New Var
      // some variables that we need
      var api = root.scrollable(),
          drawer = $("#drawer");

      // validation logic is done inside the onBeforeSeek callback
      api.onBeforeSeek(function (event, i) {

          // we are going 1 step backwards so no need for validation
          if (api.getIndex() < i || $('input[type=radio]').is(':checked')) {

              // 1. get current page
              var page = root.find(".page,.qselections").eq(api.getIndex()),

                  // 2. .. and all required fields inside the page
                  inputs = page.find(".required :input").removeClass("error"),

                  // 3. .. which are empty
                  empty = inputs.filter(function () {
                      isRadioCheck = $('input[type=radio]').is(':checked');
                      return $(this).val().replace(/\s*/g, '') == '';
                  });


              //    alert('Empty Value is bool : ' + empty.length + isRadioCheck);
              // if there are empty fields, then
              if (empty.length || !isRadioCheck) { //<======================Conditional Check

                  // slide down the drawer
                  drawer.slideDown(function () {

                      // colored flash effect
                      drawer.css("backgroundColor", "#229");
                      setTimeout(function () {
                          drawer.css("backgroundColor", "#fff");
                      }, 1000);
                  });

                  // add a CSS class name "error" for empty & required fields
                  empty.addClass("error");
                  $('.qselections').addClass("error");
                  // cancel seeking of the scrollable by returning false
                  return false;

                  // everything is good
              } else {

                  // hide the drawer
                  drawer.slideUp();
              }

          }

          // update status bar
          $("#status li").removeClass("active").eq(i).addClass("active");

      });

      // if tab is pressed on the next button seek to next page
      root.find("button.next").keydown(function (e) {
          if (e.keyCode == 9) {

              // seeks to next tab by executing our validation routine
              api.next();
              e.preventDefault();
          }
      });
  });

图像 工作亮点

在此处输入图像描述

于 2013-11-06T21:18:11.527 回答
2

您需要修改代码以处理Radio inputs没有值的代码。这是我对您的过滤功能所做的修改,希望它对您有用。

empty = inputs.filter(function() {
             if($(this).attr('type') == 'radio'){
                 if($(this).parent().hasClass('error'))
                      $(this).parent().removeClass('error');  
                 if(!$('input[name='+$(this).attr('name')+']').is(':checked'))
                 {
                   if(!$(this).parent().hasClass('error')){
                      $(this).parent().addClass('error');
                      return true;
                   }    
                 }
                 return false;
             }
             return $(this).val().replace(/\s*/g, '') == '';
});

更新

Tats 回答,他的Alertless小提琴涵盖了所有内容,更容易理解。

于 2013-11-06T21:09:27.950 回答