2016-03-28 48 views
0

我需要禁用许多不同页面上的许多窗体的提交按钮后,他们被点击,以便该窗体只能提交一次。从jQuery提交不起作用

我知道检查双重提交的正确方法是更好地完成服务器端,而不仅仅是禁用按钮,但我正在寻找一个现在应该做的简单解决方案。

这里是其中一个按钮:

<input type="submit" value="Save" /> 

这里是我写的

$(document).ready(function(){   

     $("input").each(function(index){ 
      //find all submit buttons with value "Save" 
      if($(this).attr("value")=="Save"){ 
       console.log(index); 
       $(this).click(function(){ 

       //disable the button 
       $(this).attr("disabled","true"); 
       console.log("save btn clicked"); 

       //submit the form 
       var form = $(this).closest("form"); 
       console.log(form.attr("action")); 

       form.submit();   


       }); 

      } 

     }); 



    }); 

一切工作正常,直到我在那里尝试提交部分脚本,该按钮只是禁止罚款。问题是它没有提交(它没有这个脚本)。

为什么不提交?我没有找到表单或..?

+0

如果做工精细是什么问题? –

+0

@CoderJohn忘了最基本的部分...问这个问题 – vlatkozelka

+0

你能告诉我按钮的HTML代码吗?可能是您使用

回答

0

尝试...的

$(form).submit(); 

代替

form.submit(); 
+0

之后我做了,但它仍然不起作用 – vlatkozelka

0

$('form:has(input[value="Save"])').on('submit', function() { 
 
    alert('form submitted'); 
 
    $('input[value="Save"]', this).attr('disabled', true); 
 
});
input[disabled="disabled"] { 
 
    background-color: red; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
<form method="post" action="#"> 
 
    <input type="submit" value="Save" /> 
 
</form>