2010-01-12 77 views
0

我試圖用jQuery窗體嚮導(http://www.jankoatwarpspeed.com/post/2009/09/28/webform-wizard-jquery.aspx)實現驗證腳本(bassistance),但我遇到了一些問題。jquery窗體嚮導驗證

在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); 
      }); 

     } 

有人可以幫我把這個弄清楚嗎? :)

回答

1

行,所以我加了驗證點擊事件,我想通了,「element.validate()元($(本))& & 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 */ 
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); 
    }); 
} 

更新

或者......嘗試刪除從默認勾選接收簡報?複選框(默認未選中)。 單擊事件不會連接到下一個按鈕,因爲複選框是必需的並且已被選中,所以stepIsValid爲false,並且在綁定click事件之前,createNextButton返回false。

+0

吶,它也不工作。我首先得到$(this).closest的錯誤,所以我將它改回原來的驗證代碼,它顯示了當你下一次點擊時需要什麼字段,但是一旦你填充字段並再次點擊,它不會什麼都不做 – SoulieBaby 2010-01-12 22:00:25