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

How to set default value for bootstrap drop-down list

Release: 2019-07-31 09:00:10
Original
9153 people have browsed it

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>
Copy after login

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();
Copy after login

//The value of the last option

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

//Get the value of the second option

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

//Get the selected value

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

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


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

//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());
Copy after login

//Get the length of the select

$(&#39;#test option&#39;).length;
Copy after login

//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");
Copy after login

//Add and remove Selected item

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

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

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

//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();
Copy after login

//Get the label of the first Group

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

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

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

//select is selected by default

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

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template