JQuery is definitely a great open-source JavaScript class library and a powerful tool to help us develop front-end applications quickly and efficiently. We may often process JavaScript related to forms in our daily development process. In today's code snippet sharing article, we have collected 10 extremely useful jQuery form processing codes, we hope to help you better and more quickly handle issues related to forms during development!
1. Disable the "Enter key" in the form"
You may need to prevent users from submitting forms accidentally in form operations. The following code is very helpful:
Javascript code
- $ ("# Form"). keypress (function (e ){
- If (e. which = 13 ){
- Return false;
- }
- });
Online debugging/Online demonstration
2. Clear all form data
You may need to call clear methods of different types for different form forms. However, using the following method can save a lot of effort.
Javascript code
- Function clearForm (form ){
- // Iterate over all of the inputs for the form
- // Element that was passed in
- $ (': Input', form). each (function (){
- Var type = this. type;
- Var tag = this. tagName. toLowerCase (); // normalize case
- // It's OK to reset the value attr of text inputs,
- // Password inputs, and textareas
- If (type = 'text' | type = 'Password' | tag = 'textea ')
- This. value = "";
- // Checkboxes and radios need to have their checked state cleared
- // But shoshould * not * have their 'value' changed
- Else if (type = 'checkbox' | type = 'Radio ')
- This. checked = false;
- // Select elements need to have their 'selectedindex 'property set to-1
- // (This works for both single and multiple select elements)
- Else if (tag = 'select ')
- This. selectedIndex =-1;
- });
- };
Online debugging/Online demonstration
3. Disable the buttons in the form.
The following code is very useful for ajax operations. You can effectively prevent users from submitting data multiple times, which is also frequently used by individuals:
Javascript code
- // Disable the button:
- $ ("# Somebutton"). attr ("disabled", true );
- // Start button:
- $ ("# Submit-button"). removeAttr ("disabled ");
Online debugging/Online demonstration
You may often use. attr ('Disabled ', false);, but this is an incorrect call.
4. Enter the content and enable the submit button.
This code is similar to the above, and is used to help users control the form submission button. After using this code, the submit button can be started only after you enter the specified content.
Javascript code
- $ ('# Username'). keyup (function (){
- $ ('# Submit'). attr ('Disabled ',! $ ('# Username'). val ());
- });
Online debugging/Online demonstration
5. Multiple forms cannot be submitted.
Submitting forms multiple times is a headache for web applications. The following code can help you solve this problem:
Javascript code
- $ (Document). ready (function (){
- $ ('Form'). submit (function (){
- If (typeof jQuery. data (this, "disabledOnSubmit") = 'undefined '){
- JQuery. data (this, "disabledOnSubmit", {submited: true });
- $ ('Input [type = submit], input [type = button] ', this). each (function (){
- $ (This). attr ("disabled", "disabled ");
- });
- Return true;
- }
- Else
- {
- Return false;
- }
- });
- });
Online debugging/Online demonstration
6. highlight the current highlighted input box
Sometimes you need to prompt the user's current operation input box, you can use the following code to highlight the display:
Javascript code
- $ ("Form: input"). focus (function (){
- $ ("Label [for = '" + this. id + "']"). addClass ("labelfocus ");
- }). Blur (function (){
- $ ("Label"). removeClass ("labelfocus ");
- });
Online debugging/Online demonstration
7. dynamically add form elements
This method can help you dynamically add elements in a form, such as input:
Javascript code
- // Change event on password1 field to prompt new input
- $ ('# Password1'). change (function (){
- // Dynamically create new input and insert after password1
- $ ("# Password1"). append ("<input type = 'text' name = 'password2 'id = 'password2'/> ");
- });
Online debugging/Online demonstration
8. automatically import data to selectbox
The following code uses ajax data to automatically generate content in the selection box
Javascript code
- $ (Function (){
- $ ("Select # ctlJob"). change (function (){
- $. GetJSON ("/select. php", {id: $ (this). val (), ajax: 'true'}, function (j ){
- Var options = '';
- For (var I = 0; I <j. length; I ++ ){
- Options + = '<option value = "' + j [I]. optionValue + '">' + j [I]. optionDisplay + '</option> ';
- }
- $ ("Select # ctlPerson" pai.html (options );
- })
- })
- })
Online debugging/Online demonstration
9. check whether a check box is selected.
The Code is as follows:
Javascript code
- $ ('# Checkbox'). attr ('checked ');
Online debugging/Online demonstration
10. Use code to submit the form
The Code is as follows:
Javascript code
- $ ("# Myform"). submit ();
Online debugging/Online demonstration
I hope this jQuery code will be helpful for your development. If you have similar code, please share it with us!