简体   繁体   English

单击时禁用提交按钮,但是在提交jQuery / PHP之后

[英]Disable submit button on click but after submission jQuery / PHP

At the moment I'm working on a project that requires the submission of a form for 'voting' for specific posts. 目前,我正在从事一个项目,该项目需要提交用于对特定帖子进行“投票”的表格。 At the moment clicking on the submit button works as it should, although if the button is clicked more than once, it does exactly that - submits the POST variables more than once causing them to be able to 'vote' for the item multiple times in one set of clicks. 现在,单击“提交”按钮将按其应有的方式工作,尽管如果多次单击该按钮,它确实可以做到这一点-多次提交POST变量,使它们能够在项目中多次“投票”该项目。一组点击。

I've looked at every jQuery code example I can find to solve this but nothing works. 我看过我能找到的解决每个jQuery代码的示例,但是没有任何效果。 It works to disable the button, but after that the redirection page that grabs the data and runs the queries returns an error as nothing has been submitted. 它可以禁用按钮,但是在此之后,获取数据并运行查询的重定向页面将返回错误,因为未提交任何内容。 In short, it seems to disable the button but at the same time disable the information from being submitted. 简而言之,它似乎禁用了按钮,但同时禁用了信息提交。

Here's my jQuery code: 这是我的jQuery代码:

$('#vote').submit(function(){
    $(this).children($btn).attr('disabled', true);
    return true;
});

Any help would be great. 任何帮助都会很棒。 Thanks. 谢谢。

Use jquery .one 使用jquery .one

Description: Attach a handler to an event for the elements. 说明: 将处理程序附加到元素的事件。 The handler is executed at most once per element. 该处理程序每​​个元素最多执行一次。

 $(document).one("click","#vote",function(){
        $(this).children($btn).attr('disabled', true);
        return true;
    });

Probably your best option is to only allow a single submission and adjust the button appearance some other way: 最好的选择可能是只允许一次提交并以其他方式调整按钮外观:

var submitted = false;
$('#vote').submit(function(){
  if(submitted) {
    // cancel additional submits
    return false;
  }

  submitted = true;
  $(this).children($btn).val('Please wait...');
});

Jquery's on and off can be used here. jQuery的打开关闭可在此处使用。

for example after submission,you can completely disable the click by 例如,提交后,您可以完全禁用点击

$('#vote').off('click');

and then switch it back if you want by 然后根据需要将其切换回

$('#vote').on('click');

you could add an click event . 您可以添加click event Instead of using submit button use a button click event. 代替使用提交按钮,而是使用按钮单击事件。

the code might look like this 代码可能看起来像这样

$($button).click(function(){
$(this).attr("disabled","disabled");
$($form).submit();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM