2011-04-25 39 views
0

我有此代碼段將提交一個表單一次,以防止多個數據庫插入:jQuery的:.validate()函數,並提交一旦功能

 // Validates the form data 
     // Running before submit, otherwise HTTP process starts and we POST 
     $(".empForm").validate(); 
     // Setting the submit button to enabled 
     $("input[type='submit']").attr("disabled", false); 
     // disabling the submit button after it's been pressed atleast once 
     // Prevents duplicate entries in the database 
     $(".empForm").submit(function(){ 
      // if it's valid, lock the submit button 
      // if the form is not valid, they can complete and try again 
      if($(".empForm").valid()){ 
       $("input[type='submit']").attr("disabled", true).val("Please wait..."); 
       return true; 
      }// closes if 
     }); //closes submit 

問題是我使用.validate()函數的jQuery和當表單返回爲缺少字段時,我無法提交表單,因爲它被禁用。有什麼建議麼?

回答

0

我解決了這個一個。

$("input[type='submit']").click(function(){ 
    if($(".empForm").valid()==true){ 
    $("input[type='submit']").attr("disabled", true).val("Please wait...");} 
     else{$("input[type='submit']").attr("disabled", false);} 
}); 
0

,而不是禁用它,打開它只讀

Example

('#readonly').click( 
function() 
{ 
    if ($('#readonly').attr("readonly") == true) 
    { 
     $('#readonly').val(''); 
     $('#readonly').removeAttr("readonly"); 
    } 
}); 

對於你的具體情況:(未測試)

$("input[type='submit']").removeAttr("readonly"); 
//or $("input[type='submit']").attr('readonly', false) 
// disabling the submit button after it's been pressed atleast once 
     // Prevents duplicate entries 
     $("form").submit(function(){ 
      $("input[type='submit']").attr('readonly', true).val("Please wait..."); 
      return true; 
     }); 
+0

謝謝你。你可以看看我編輯的代碼,看看是否可以代替嗎? – 2011-04-27 17:40:17

2

使用提供的jQuery/Validate提交處理程序。

$(".selector").validate({ 
    submitHandler: function(form) { 
    $("input[type='submit']").attr("disabled", true).val("Please wait..."); 
    form.submit(); 
} 
}) 

SubmitHandler只在窗體有效的情況下運行,所以這應該可以解決您的問題。