Disabling the submit button after clicking the Close button,

One for puzzles.

I want to disable the submit button to prevent duplicate submissions.

For this, I use jQuery:

jQuery('#subnewtopicform').on('submit', function () {
    jQuery('#subnewtopic').prop('disabled', true);
});

And removed the HTML:

<form action="file.php" method="post" id="subnewtopicform">
    <input type="text" name="title" />
    <input type="submit" value="Submit" name="subnewtopic" id="subnewtopic" /> 
</form>

When you click the Submit button, it turns off and the page reloads, but the form does not submit properly, why ?! Removing jQuery code will lead to its correct presentation.

In my 10 years of coding, I have yet to meet something strange.

+4
source share
2 answers

Try this code. I am not getting an error message.

<form action="file.php" method="post" id="subnewtopicform">
    <input type="text" name="title" />
    <input type="submit" value="Submit" name="subnewtopic" id="subnewtopic" /> 
</form>

<script src="assets/js/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
    $('#subnewtopicform').on('submit', function(e) {
        $('#subnewtopic').prop('disabled', true);
        // e.preventDefault();
    });
</script>

What you need to remember:

  • POST , . e.preventDefault() $("#subnewtopic.prop('disabled', true);, , .
  • jQuery , .
  • Firefox Chrome Javascript "", , - .

, . , , , , , , - . , !

+3

true jquery ( )

UPDATE:

jQuery('#subnewtopicform').on('submit', function () {
    jQuery('#subnewtopic').prop('disabled', true);
    return true; 
});

:
Javascript Best Practice?

, , , . .

-3

Source: https://habr.com/ru/post/1570356/


All Articles