2012-12-04 94 views
0

我使用表單驗證這段代碼,的jQuery/JavaScript表單驗證工作不

$("#btnaddregistration").live("click", function(e) { 
    e.preventDefault(); 
    var errorCount = 0; 
    $("#formaddregistration").find("input").each(function(i) { 
     if ($(this).val().length == 0) { 
      errorfield = $(this); 
      errorCount++; 
      $("#modaldialogtext").text("'" + $(this).parent().parent().find("label").text() + "' cannot be left blank."); 
      $("#modal_confirmation").dialog("open"); 
      return false; 
     } 
    }); 
    if (errorCount > 0) { 
     return false; 
    } else { 
     document.forms["formaddregistration"].submit(); 
    } 
});​ 

但形式不提交。我使用了所有可能的提交方法,但是我失敗了。任何想法我錯了?

+5

你試過'$( 「#formaddregistration」)。submit()'yet?畢竟你有jQuery可用。 – Sparky

+0

你是否在控制檯部分有任何錯誤 –

+0

雅男人,我試過$(「#formaddregistration」)。 。它表明f [a]不是一個函數。對於這段代碼,我得到的document.form.formaddregistration.submit不是一個函數。即使我有回報的真實;但仍然不適合我。 – Shyantanu

回答

-1

首先,您可以改善您的代碼,這將有助於您排除故障。

如果沒有提交表單,請確保選擇的實際工作,即,嘗試像$form.hide(),看看它是否隱藏,以確保它不是PEBCAK

var $form = $("#formaddregistration"); 
$("#btnaddregistration").on("click", function(e) { 
    var errorCount = 0; 


    $form.find("input").each(function(i) { 
     var $this = $(this); 
     if ($this.val().length < 1) { 
      errorCount++; 

      // this line also needs improving.. 
      $("#modaldialogtext").text("'" + $this.parent().parent().find("label").text() + "' cannot be left blank."); 
      $("#modal_confirmation").dialog("open"); 
      return false; 
     } 
    }); 

    if (errorCount < 1) { 
     $form.submit(); 
    } 

    return false; 
});​ 
+0

查看OP的最新評論。如果'$(「#formaddregistration」)。submit();'不適合他,你的解決方案也不會。 – Sparky

+0

不,但清理他的代碼應該可以幫助他更容易地進行調試。實際上,他多次獲取表單dom元素,這本身就是一個相當大的問題。我同意 – Eva

+1

,但是,答案部分僅用於解答......這就是爲什麼你是唯一一個發佈這樣的人遠。否則,我們會有十個答案,每個答案都有不同的建議,猜測或疑難解答提示。 – Sparky