如何防止使用jQuery或Javascript进行双重提交? [英] How can I prevent a double submit with jQuery or Javascript?

查看:108
本文介绍了如何防止使用jQuery或Javascript进行双重提交?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我使用Google搜索并发现了一些脚本,但是没有一个脚本出现在我的数据库中,因为我的数据库中存在重复条目,因为不耐烦的用户多次单击提交按钮。他们似乎是足够的。



我怎样才能防止这些重复的条目发生使用JavaScript或最好jQuery?



Thanx提前!

解决方案

如何禁用提交按钮?我就是做这个的。它可以正常工作。

$ $ $ $'code $('form')。submit(function(){
$('input [ type = submit]',this).attr('disabled','disabled');
});

免责声明:

这仅适用于在用户浏览器中启用javascript的情况。如果提交的数据非常重要(比如购买信用卡),那么请将我的解决方案视为第一道防线。对于很多用例,禁用提交按钮将提供足够的预防。



我会首先实现这个仅限javascript的解决方案。然后跟踪有多少重复记录仍在创建。如果它为零(或者足够低以至于不在意),那么你就完成了。如果它对您而言太高,则对现有记录实施后端数据库检查。

I keep getting duplicate entries in my database because of impatient users clicking the submit button multiple times.

I googled and googled and found a few scripts, but none of them seem to be sufficient.

How can I prevent these duplicate entries from occurring using javascript or preferably jQuery?

Thanx in advance!

解决方案

How about disabling the button on submit? That's what I do. It works fine.

$('form').submit(function(){
    $('input[type=submit]', this).attr('disabled', 'disabled');
});

Disclaimer:
This only works when javascript is enabled on the user's browser. If the data that's being submitted is critical (like a credit card purchase), then consider my solution as only the first line of defense. For many use cases though, disabling the submit button will provide enough prevention.

I would implement this javascript-only solution first. Then track how many duplicate records are still getting created. If it's zero (or low enough to not care), then you're done. If it's too high for you, then implement a back-end database check for an existing record.

这篇关于如何防止使用jQuery或Javascript进行双重提交?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆