jquery相當新,但我意識到我的代碼可以被壓縮。它工作得很好,但有一個更好的方法來做到這一點,因爲它看起來像冗餘代碼。我有兩個輸入字段和一個我正在驗證的textarea,見下文。代碼樣本將是理想的,因爲我還在學習,但任何幫助appreiacted:jQuery驗證多個表單域
$("form#designForm").submit(function(){
if($("#ccDesignFirstName").val().length == 0){
alert("Full Name field is required");
$("#ccDesignFirstName").focus();
return false;
}
if($(".telephoneInput").is(":visible")){
if($("#ccDesignTelephone").val().length == 0){
alert("Phone is required");
$("#ccDesignTelephone").focus();
return false;
}
}
if($("#ccDesignProblem").val().length == 0){
alert("Question is required");
$("#ccDesignProblem").focus();
return false;
}
});
確實是多餘的。你應該看看函數,數組和循環,所以基本上學習Javascript。 jQuery只不過是一個幫助DOM和Ajax的工具。 – kapa 2013-05-09 22:03:27