How to operate select with jquery

The colon is used here, and mastering its usage will also make the code concise. Many times the select cascade is used, that is, the value of the second select changes with the value selected by the first select. This is very simple in jquery.







































Such as:

1

2

3

4

5

6

7

$(".selector1").change(function(){ // First empty the second $(".selector2").empty(); // In actual application, here The option is generally generated by looping multiple var option = $(""); //Add an Option (drop-down item) to Select 2. $("#select_id").prepend(""); // Select insert an Option (first position) 3. $("#select_id option:last").remove(); //Delete the largest index value in Select Option (last one) 4. $("#select_id option[index ='0']").remove(); //Delete the Option with index value 0 in Select (the first one) 5. $("#select_id option[value='3']").remove(); //Delete Option 5. Value='3' in Select. $("#select_id option[text='4']").remove(); //Delete Text='4' in SelectOption jquery radio value, checkbox value, select value, radio selected, checkbox selected, select selected, and related





















Get the value of a selected radio item

1

var item = $('input[name=items][checked]').val();

Get the text of the selected item

1

var item = $("select[name= items] option[selected]").text();

The second element of the select drop-down box is the currently selected value

1

$('#select_id')[0].selectedIndex = 1;

The second element of the radio group Is the currently selected value

1

$('input[name=items]').get(1).checked = true;

Get value:

text box, text area: $("#txt").attr("value");

Multi-select box checkbox: $("#checkbox_id").attr("value");

single-

select group radio: $("input[type=radio][checked]").val(); drop-down box select: $( '#sel').val();

Control form elements:

text box, text area: $("#txt").attr("value",'');//Empty content

$("#txt").attr("value",'11');//Fill in the content

Multi-select box checkbox: $("#chk1").attr("checked",'');//Do not tick

$("#chk2").attr("checked",true);//check

if($("#chk1").attr('checked')==undefined) // judge whether it has been checked

single check Group radio: $("input[type=radio]").attr("checked",'2');//Set the item with value=2 as the currently selected item

drop-down box select: $("#sel"). attr("value",'-sel3');//Set the item with value=-sel3 as the currently selected item

$("").appendTo("#sel")//Add the option of the drop-down box

$("#sel ").empty();//Empty the drop-down box

Guess you like

Origin blog.csdn.net/mjian178/article/details/112856770