限制表单可以提交的时间 [英] Limit the number of time a form can be submitted

查看:83
本文介绍了限制表单可以提交的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个textarea:

 < form id ='test'> 
< textarea class ='test2'> < / textarea的> < input type ='submit'value ='send'/>
< / form>

我想知道是否有办法限制用户可以提交的次数textarea(例如,每分钟5次)。



编辑:
我想在客户端使用Javascript。



编辑2:



这是一个聊天。我只是不希望人们能够在一小段时间内发布太多的消息。因此,如果他们刷新页面以获得极限,那么他们花费的时间足够长,这不是问题。

解决方案

您可以存储一些变量:

  $(function(){
var timesSubmitted = 0;
var maxSubmits = 5;
var intervalMilliseconds = 10000; //测试
var interval;
$('input [type = submit]')。click(function (event){
if(!interval){
interval = setTimeout(function(){
interval = undefined;
timesSubmitted = 0;
$('div ').append('TIMER RESET。再次提交。< br />')
},intervalMilliseconds);
}
timesSubmitted ++;
if(timesSubmitted> ; maxSubmits){
$('div')。append('NO GO YO!< br />')
} else {
$('div')。append( '有效的< br /> ')
}
event.preventDefault();
});
});

示例


I have a textarea:

      <form id='test'>
      <textarea class='test2'> </textarea> <input type='submit' value='send'/>
      </form>

I would like to know if there is a way to limit the number of times a user can submit the textarea (for example, 5 times per minute).

EDIT: I would like to do that on client-side, in Javascript.

EDIT 2:

It is for a chat. I just don't want people to be able to post too many messages within a small lap of time. So it's not a problem if they refresh their page to get ride of the limit, the time they'll take to do it is long enough...

解决方案

You can store some variables:

$(function () {
   var timesSubmitted = 0;
   var maxSubmits = 5;
   var intervalMilliseconds = 10000; // for testing   
   var interval;   
    $('input[type=submit]').click(function (event) {
        if (!interval) {
            interval = setTimeout(function () {
                interval = undefined;
                timesSubmitted = 0;
                $('div').append('TIMER RESET. Submit again.<br />')
            }, intervalMilliseconds);
        }
        timesSubmitted ++;
        if (timesSubmitted > maxSubmits) {
            $('div').append('NO GO YO!<br />')
        } else {
            $('div').append('valid<br />')   
        }
        event.preventDefault();        
    });        
});​

Example

这篇关于限制表单可以提交的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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