Home  >  Article  >  Web Front-end  >  Example of jQuery getting the text and value selected by select_jquery

Example of jQuery getting the text and value selected by select_jquery

WBOY
WBOYOriginal
2016-05-16 17:10:141132browse

Get select:
Get selected text:
$("#ddlregtype").find("option:selected").text();

Get selected value:
$("#ddlregtype ").val();

Get the selected index:
$("#ddlregtype ").get(0).selectedindex;

Set select:
Set select selected index:
$("#ddlregtype ").get(0).selectedindex=index;//index is the index value

Set select selected value:

Copy code The code is as follows:

$("#ddlregtype ").attr("value" ,"normal");
$("#ddlregtype ").val("normal");
$("#ddlregtype ").get(0).value = value;

Set select selected text:
Copy code The code is as follows:

var count= $("#ddlregtype option").length;
for(var i=0;i{ if($("#ddlregtype ").get(0).options[i] .text == text)
{
$("#ddlregtype ").get(0).options[i].selected = true;
break;
}
}
$("#select_id option[text='jquery']").attr("selected", true);

Set the select option item:
Copy code The code is as follows:

$("#select_id").append(""); //In Insert an option in front
$("#select_id option:last").remove(); //Remove the option with the largest index value
$("#select_id option[index='0']"). remove();//Delete the option with index value 0
$("#select_id option[value='3']").remove(); //Delete option with index value 3
$(" #select_id option[text='4']").remove(); //Delete option with text value 4

Clear select:

$("#ddlregtype " ).empty();

The work needs to get the values ​​​​in the two forms. As shown in the picture:

How to get the value added from the left selection box to the right selection box? I thought about using web page special effects to get it, and the more popular jquery was used here.
The js code is as follows:
Copy code The code is as follows:

//Get all attributes Value var item = $("#select1").val();
$(function(){
$('#select1').each( //Get all values ​​of select1
function( ){
$('button').click(function(){
alert($('#select2').val()); //Get the select1 value in select2
});
});
})


It is worth noting that it cannot be written directly as
Copy code The code is as follows:

$(function(){
$('#select2').each( //Get all values ​​of select1 , because as mentioned above, options are added from the left to the right, jquery does not actually pass the value from the left to the right.
function(){
$('button').click(function(){
alert($(this).val()); //Get the select1 value in select2
});
});
})

html:
Copy code The code is as follows:




Select Add to the right>>
Add all to the right>>





<