← Back to jQuery Course | Chapter 8: Working with Forms | Lesson 5 of 5

Double Submissions रोकना

Processing शुरू होते ही submit button को disable करें।
Syntax
javascript
$(form).one("submit", handler);
$(form).find(":submit").prop("disabled", true);

Submit के बाद Disable करना

Form submit होते ही submit button को disable करना user को page respond करने से पहले फिर click करके गलती से इसे दो बार भेजने से रोकता है।

उदाहरण: Disable After Submit

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <form id="myForm"><button id="submitBtn" type="submit">Send</button></form>
    <script>
      $("#myForm").on("submit", function() {
      $("#submitBtn").prop("disabled", true);
      });
    </script>
  </body>
</html>

Submission Lock

submit handler की शुरुआत में check किया गया एक boolean flag repeated submit handling को रोक सकता है भले ही disable-the-button approach कहीं bypass हो जाए।

उदाहरण: Submission Lock

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <form id="myForm"><button type="submit">Send</button></form>
    <script>
      let submitted = false;
      $("#myForm").on("submit", function(event) {
      if (submitted) {
      event.preventDefault();
      return;
      }
      submitted = true;
      });
    </script>
  </body>
</html>

Button Feedback

Processing के दौरान button का text बदलकर कुछ 'Submitting...' जैसा कर देना users को बताता है कि उनका action register हो गया है और कुछ हो रहा है, सिर्फ button को greyed out छोड़ने के बजाय।

उदाहरण: Button Feedback

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <form id="myForm"><button id="submitBtn" type="submit">Send</button></form>
    <script>
      $("#myForm").on("submit", function() {
      $("#submitBtn").text("Submitting...");
      });
    </script>
  </body>
</html>

Async Submission

खासकर AJAX submissions के लिए, button तब तक locked रहना चाहिए जब तक request असल में complete न हो जाए -- success हो या failure -- क्योंकि page कभी reload नहीं होता accidental reset के लिए।

उदाहरण: Async Submission

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <button id="submitBtn">Send</button>
    <script>
      $("#submitBtn").on("click", function() {
      $(this).prop("disabled", true);
      setTimeout(function() {
      $("#submitBtn").prop("disabled", false);
      }, 1000);
      });
    </script>
  </body>
</html>

Complete Protection

Validation, एक submission lock, और button feedback को combine करना हर angle से double-submission को cover करता है: bad input को भेजने से पहले पकड़ना, repeated handling को block करना, और user को inform रखना।

उदाहरण: Complete Protection

javascript
<!DOCTYPE html>
<html>
  <head>
    <script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
  </head>
  <body>
    <form id="myForm"><input id="name"><button id="submitBtn" type="submit">Send</button></form>
    <script>
      $("#myForm").on("submit", function(event) {
      if ($("#name").val().trim() === "") {
      event.preventDefault();
      return;
      }
      $("#submitBtn").prop("disabled", true).text("Submitting...");
      });
    </script>
  </body>
</html>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. submit button को उसके click handler में disable करना बजाय form के submit event में, ताकि Enter press करने से form फिर भी दो बार submit हो सके।
  2. एक AJAX submit में button को disable करना और error पर कभी re-enable न करना, form को stuck छोड़ते हुए; complete callback या .always() इस्तेमाल करें।
  3. handler के अंदर let sending = false जैसा एक lock flag set करना, जहाँ यह हर call पर reset हो जाता है, इसे बाहर रखने के बजाय ताकि यह persist करे।
चैप्टर सारांश
  • jQuery जटिल form inputs handle करता है और form state पढ़ता और बदलता है।
  • जैसे-जैसे user interact करता है forms को dynamically बदला जा सकता है।
  • Client-side validation और double submissions रोकना form behavior बेहतर बनाते हैं।
🔒

Chapter Quiz — Complete all 5 topics to unlock

0/5 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.