2011-04-25 38 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只在窗体有效的情况下运行,所以这应该可以解决您的问题。