JQuery gets an example of the text and value of the select selection

  • 2020-03-30 00:46:02
  • OfStack

For the select:
Get the text selected by select:
$(" # ddlregtype "). The find (" option: selected "). The text ();

Get the value selected by the select:
$(" # ddlregtype "). Val ();

Gets the index selected by select:
$(" # ddlregtype "). The get (0). Selectedindex;

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

Set the selected value of select:
 
$("#ddlregtype ").attr("value","normal " ); 
$("#ddlregtype ").val("normal"); 
$("#ddlregtype ").get(0).value = value; 

Set the text selected by select:
 
var count=$("#ddlregtype option").length; 
for(var i=0;i<count;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:
 
$("#select_id").append("<option value='value'>text</option>"); //Add an option
$("#select_id").prepend("<option value='0'> Please select a </option>"); //Insert an option in front
$("#select_id option:last").remove(); //Drop the option with the largest index value
$("#select_id option[index='0']").remove();//Deletes the option with an index value of 0
$("#select_id option[value='3']").remove(); //Delete the option with a value of 3
$("#select_id option[text='4']").remove(); //Delete the option with a text value of 4

To empty the select:

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

The job is to get the values in both forms. As shown in figure:

How do I get the values added from the left selection box to the right selection box? I wanted to use the web effects can be obtained, here using the popular jquery.
Js code is as follows:
 
//Var item = $("#select1").val();
$(function(){ 
$('#select1').each( //Gets all the values for select1
function(){ 
$('button').click(function(){ 
alert($('#select2').val()); //Gets the value of select1 in select2
}); 
}); 
}) 
</script> 

It's important to note that you can't just write this
 
$(function(){ 
$('#select2').each( //Get all of the values for select1, because jquery doesn't really pass the values from the left to the right.
function(){ 
$('button').click(function(){ 
alert($(this).val()); //Gets the value of select1 in select2
}); 
}); 
}) 

HTML:
 
<div class="centent"> 
<select multiple="multiple" id="select1" name="dd" style="width:100px;height:160px;"> 
<option value="1"> options 1</option> 
<option value="2"> options 2</option> 
<option value="3"> options 3</option> 
<option value="4"> options 4</option> 
<option value="5"> options 5</option> 
<option value="6"> options 6</option> 
<option value="7"> options 7</option> 
</select> 
<div> 
<span id="add" > Select add to the right >></span> 
<span id="add_all" > Add them all to the right >></span> 
</div> 
</div> 
<div class="centent"> 
<select multiple="multiple" id="select2" name="sel" style="width: 100px;height:160px;"> 
</select> 
<div> 
<span id="remove"><< Select and delete to the left </span> 
<span id="remove_all"><< Delete everything to the left </span> 
</div> 
</div> 

With JQuery, Ajax calls dynamically populate the options of the Select
 
//Bind the ClassLevel1 click event
$("#ClassLevel1").change(function () { 
var id = $("#ClassLevel1").val(); 
var level2 = $("#ClassLevel2"); 
level2.empty(); 
$("#ClassLevel3").hide(); 
$.ajax({ 
url: "./askCommon.ashx?action=getclasslevel&pid=" + id, 
data: { "type": "ajax" }, 
datatype: "json", 
type: "get", 
success: function (data) { 
var json = eval_r(data); 
for (var ind in json) { 
level2.append($("<option value='" + json[ind].id + "'>" + json[ind].typename + "</option>")); 
} 

} 
}); 
}) 

Related articles: