Simple code for practical jquery operation of form elements

  • 2021-07-02 22:54:50
  • OfStack

Take the text of the selected item in the drop-down menu; Gets and sets the value of the drop-down menu; Empty the drop-down menu; Add elements to the following menus; Take the radio box value; Selection of radio or check buttons; Take the check box value; Judging whether the radio or check box is selected; Element is available or unavailable; Determines whether the element is available or not.

Practical jquery operation form element code


/*

 Assuming that the 1 There are 1 Buttons id="save"
$(document).ready(function(){
   $("#save").click(function(){
    $("#save").attr("disabled",true);// Set to unavailable              
    $("#form1")[0].submit();// If you have many id For form1 It doesn't matter if the form of 1 I will submit it. Ha ha .
   });
});

Take the text of the selected item in the drop-down menu;

Gets and sets the value of the drop-down menu;

Empty the drop-down menu;

Add elements to the following menus;

Take the radio box value;

Selection of radio or check buttons;

Take the check box value;

Judging whether the radio or check box is selected;

Element is available or unavailable;

Determines whether the element is available or not.

1. Fetch the text of the selected item in the drop-down menu


$("#select option[selected]").text();//select And option There is a space between them, option For select Child elements of   
$("#select option:selected").text();// If it is written as $("#select").text(); Will select the text of all drop-down menus   

2. Gets and sets the value of the drop-down menu


$("#select").val();// Value   
$("#select").val("value");// Settings , If select Has a value of value Options for , This option will be selected , If it doesn't exist , Then select Do not make any changes  

3. Empty the drop-down menu


$("#select").empty();  
$("#select").html(""); 

4. Add elements to the following menus


$('<option value="1">1</option>').appendto($("#select"));  
$("#select").append('<option value="1">1</option>'); 

5. Take radio box values


$("#id[checked]").val(); 

6. Radio or check button selection


$("#id[value=val]").attr("checked",true);// Select   
$("#id[value=val]").attr("checked","");// Deselect   
$("#id[value=val]").attr("checked",false);// Deselect   
$("#id[value=val]").removeattr("checked");// Deselect  

7. Take check box values


$("input[type=checkbox][checked]").each(function(){  
alert($(this).val());  
})  

// If you use $("input[type=checkbox][checked]").val(), Will only return the 1 Selected values  

8. Determine if a radio or check box is selected


if($("#id").attr("checked")){}// Judge selected   
if($("#id").attr("checked")==true){}// Judge selected   
if($("#id").attr("checked")==undefined){}// Judge unselected  

9. Element Available or Not Available


$("#id").attr("disabled",false);// Make available   
$("#id").attr("disabled",true);// Set to unavailable  

10. Determine whether the element is available or not


$("#select option[selected]").text();//select And option There is a space between them, option For select Child elements of   
$("#select option:selected").text();// If it is written as $("#select").text(); Will select the text of all drop-down menus   
0

Related articles: