drh96824
2015-05-29 15:14
浏览 59
已采纳

jquery / ajax - 单击任何表单上的“提交”按钮3秒钟

I have searched and searched and found nothing that explains what I need.

I have a webscript that has dozens of forms all over the place. I need to disable the submit button for 3 seconds after submit is clicked on any form no matter what the id, class, or action of the form.

I found the following code:

$('#btn').click(function(){
    var btn = $(this);
    $.post('http://someurl.com',{delay: 3}).complete(function(){
btn.prop('disabled', false);
    });
btn.prop('disabled', true);

});

but this requires the form have an id of btn and it appears to require the form be "posted" to some page.

Is there a way to achieve this on all forms reguardless of the action being performed? So if its a signup form, it disables the "join" button for 3 seconds, if its a comment form, it equally disables the submit for 3 seconds after submitting comment, .... basically no matter what the form or action I want to disable the submit button for 3 seconds after click.

Is this even possible?

I have tried changing all instances of "#btn" to "input" but this doesn't seem to work. I am really not great at this stuff but seem to manage stumbling through things with the help of others.

The reason my question is NOT a duplicate of other questions is because I specifically stated "no matter what the id, class, or action of the form". The solutions presented on the other question do not specifically address this. Adding id's, classes, or the likes is not a remote possibility on a webscript with hundreds and hundreds of forms for various reasons (its a social networking script). The solution I marked as correct is the solution that did not require any attributes to be added to the html elements.

图片转代码服务由CSDN问答提供 功能建议

我搜索并搜索过,没有找到解释我需要的内容。

我有一个webcript,遍布整个地方有几十种形式。 无论表单的id,类或操作是什么,我都需要在任何表单上单击提交后禁用提交按钮3秒钟。

我找到了以下代码:

  $('#btn')。click(function(){
 var btn =  $(this); 
 $ .post('http://someurl.com',{delay:3})。complete(function(){
btn.prop('disabled',false); 
})  ; 
btn.prop('disabled',true); 
 
}); 
   
 
 

但是这需要表单的id为btn并且它出现 要求将表单“发布”到某个页面。

有没有办法在所有形式上实现这一点,无论正在执行的操作如何? 因此,如果它是一个注册表单,它会禁用“加入”按钮3秒,如果它是一个评论表单,它在提交评论后同样禁用提交3秒,....基本上无论我想要什么形式或行动 单击后禁用提交按钮3秒钟。

这甚至可能吗?

我尝试将“#btn”的所有实例更改为“输入”,但这似乎不起作用。 我真的不擅长这些东西,但似乎在其他人的帮助下管理绊脚石。

我的问题与其他问题不重复的原因是因为我特别说“不” 无论形式的id,类或行为是什么“。 在另一个问题上提出的解决方案没有具体解决这个问题。 由于各种原因(其社交网络脚本),添加id,类或类似内容并不是具有数百种表单的webscript的远程可能性。 我标记为正确的解决方案是不需要将任何属性添加到html元素的解决方案。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题