2011-05-08 148 views
1

我有以下代碼:的jQuery呼叫功能

var x; 
x=$(document); 
x.ready(initEvents); 

function initEvents() 
{ 
    var x; 
    x = $("#send"); 
    x.click(pressButton); 
    x = $("#clean"); 
    x.click(cleanForm); 
} 

/*this method validates the form*/ 
function pressButton() 
{ 
    $("#form1 :input").attr("disabled", true); 

    $("#form1").validate(
    { rules: 
     { 
      //sets the rules for validate 
     }, 
     messages: 
     { 
      //sets the message for validates 
     }, 
     submitHandler: function (form) 
     { 
      //send the form 
     } 
    }); 
} 

我的問題是,如果把這個線$("#form1 :input").attr("disabled", true);的只會驗證不執行,但如果我評論的驗證功能的偉大工程。問題是爲什麼我不能執行這兩行?我在代碼中做了什麼錯誤?

+4

這可能是因爲您的驗證插件不確實是'disabled'過程表單元素。 – drudge 2011-05-08 02:02:03

+2

你的意思是*驗證不執行*?我的猜測是禁用的元素不會被檢查,因爲它們不會被髮送到服務器。 – 2011-05-08 02:02:49

+0

@Felix Kling感謝您的詢問,當我談論驗證時,請不要使用驗證 – Jorge 2011-05-08 02:07:51

回答

0

我解決我的問題是這樣,禁用表單驗證把線在驗證

/*this method validates the form*/ 
function pressButton() 
{ 


$("#form1").validate(
{ rules: 
    { 
     //sets the rules for validate 
    }, 
    messages: 
    { 
     //sets the message for validates 
    }, 
    submitHandler: function (form) 
    { 
     //disable the form 
     $("#form1 :input").attr("disabled", true); 
     //send the form 
    } 
}); 
} 
1

我不認爲這是你的實際問題,這裏的submithanlder後,但值得注意的是,你應該注意getting your curly braces right when you're working with JavaScript。在鍵名稱和值的左括號之間添加換行符可能會導致鍵名在某些情況下被解釋爲a JavaScript label

這將是更好的一般都採用這種風格,以避免陷入莫名其妙的錯誤以後:

// Why complicate this? 
$(document).ready(initEvents);  

function initEvents() { 
    $("#send").click(pressButton); 
    $("#clean").click(cleanForm); 
} 

/*this method validates the form*/ 
function pressButton() { 
    $("#form1 :input").attr("disabled", true); 

    $("#form1").validate({ 
    rules: { 
     // the rules for validate 
    }, 
    messages: { 
     //sets the message for validates 
    }, 
    submitHandler: function (form) { 
     //send the form 
    } 
    }); 
} 
+0

感謝您的提示我真的很感激 – Jorge 2011-05-08 02:40:38