1

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.

I've looked at every jQuery code example I can find to solve this but nothing works. 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:

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

Any help would be great. Thanks.

4 Answers 4

3

Use 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;
    });
Sign up to request clarification or add additional context in comments.

4 Comments

what if i want to really re submit the form?
@passionateCoder you can't do it instant.you have to refresh the page and try again.To use more than one you want to use .on instead of .one
I'm unsure of how to fully use this code. Would you be able to give a functioning example? Thanks for your help.
Nope, that's still not working unfortunately. Any other suggestions?
1

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...');
});

Comments

1

you could add an 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();
});

Comments

1

Jquery's on and off can be used here.

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');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.