Home  >  Article  >  Web Front-end  >  How to set default value for bootstrap drop-down list

How to set default value for bootstrap drop-down list

尚
Original
2019-07-30 17:40:489105browse

How to set default value for bootstrap drop-down list

Get the value from the drop-down box and set the default value:
##

<select id="test">
<option value="1">选项一<option>
<option value="2">选项一<option>
...
<option value="n">选项N<option>
</select>

The so-called jQuery operation "select", To be more precise, jQuery should control "option". Look at the jQuery code below:

//Get the value of the first option

$(&#39;#test option:first&#39;).val();

//The value of the last option

$(&#39;#test option:last&#39;).val();

//Get the value of the second option

$(&#39;#test option:eq(1)&#39;).val();

//Get the selected value

$(&#39;#test&#39;).val();
$(&#39;#test option:selected&#39;).val();

//Set the option with a value of 2 to the selected state


$(‘#test’).attr(‘value’,’2’);

//Set the last option to be selected

$(&#39;#test option:last&#39;).attr(&#39;selected&#39;,&#39;selected&#39;);
$("#test").attr(&#39;value&#39; , $(&#39;#test option:last&#39;).val());
$("#test").attr(&#39;value&#39; , $(&#39;#test option&#39;).eq($(&#39;#test option&#39;).length - 1).val());

//Get the length of the select

$(&#39;#test option&#39;).length;

//Add an option

$("#test").append("<option value=&#39;n+1&#39;>第N+1项</option>");
$("<option value=&#39;n+1&#39;>第N+1项</option>").appendTo("#test");

//Add and remove Selected item

$(&#39;#test option:selected&#39;).remove();

//Delete the selected item (the first item is deleted here)

$(&#39;#test option:first&#39;).remove();、

//The specified value is deleted

$(&#39;#test option&#39;).each(function(){
if( $(this).val() == &#39;5&#39;){
$(this).remove();
}
});
$(&#39;#test option[value=5]&#39;).remove();

//Get the label of the first Group

$(&#39;#test optgroup:eq(0)&#39;).attr(&#39;label&#39;);

//Get the value of the first option under the second group

$(&#39;#test optgroup:eq(1) : option:eq(0)&#39;).val();

//select is selected by default

$("#arrangeClass").find("option[value="+reim.classId+"]").prop("selected",true);

Recommended:

bootstrap introductory tutorial

The above is the detailed content of How to set default value for bootstrap drop-down list. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn