Table of Contents
Introduction
हेलो दोस्तों आज हम इस post में form को enter key press पर submit कराएँगे!. आम तोर पर form को हम submit button के click पर submit करते है!. तो इस post में हम form को enter keyPress, keydown, keyup जैसे method से form को submit कराएँगे!
jQuery का इस्तेमाल करके Enter button पर form submit करने के लिए कई methods available है! आये Enter पर form submit करने में इस्तेमाल किये जाने वाले कुछ methods के बारेमे जानते है!.
HTML form
<form id="myForm"> <input type="text" id="textInput"> <button type="submit">Submit</button> </form>
हमारी पास एक form है जो आप ऊपर example में देख सकते है!.
Method 1: Using Keydown Event
<script> $(document).ready(function(){ $('#textInput').keydown(function(e){ if(e.keyCode == 13) { $('#myForm').submit(); return false; } }); }); </script>
ऊपर के example में देख सकते है! यह method text input field पर keydown event पे चलाया गया है!. जब Enter key (keyCode 13) pressed किया जाता है!, तो यह $(‘#myForm’).submit() का इस्तेमाल करके form submission को trigger करता है!.
Method 2: Using Keypress Event
<script> $(document).ready(function(){ $('#textInput').keypress(function(e){ if(e.which == 13) { $('#myForm').submit(); return false; } }); }); </script>
ऊपर के example में देख सकते है पहले method keydown के सामान, लेकिन यह keypress event का इस्तेमाल करता है!. keycode get करने के लिए e.keyCode के बजाय e.which इस्तेमाल किया जाता है!.
Method 3: Using Keyup Event
<script> $(document).ready(function(){ $('#formId').on('keyup', function(e){ if(e.key === "Enter") { e.preventDefault(); $(this).submit(); } }); }); </script>
ऊपर के exmaple में देख सकते है की ऊपर के दोनों method के सामान ही keyup event का इस्तेमाल करके form को submit करा है! इस event में keycode get करने के लिए e.key इस्तेमाल किया है!.