Home > Article > Web Front-end > How to set default value for drop-down box in JS?
#How to set the default value for the drop-down box in JS?
First create a standard HTML5 document;
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> </body> </html>
Then add a drop-down box in the document;
<select name="users"> <option value="1">User 1</option> <option value="2">User 2</option> <option value="3">User 3</option> <option value="4">User 4</option> <option value="5">User 5</option> </select>
Then introduce jQuery into the page;
<script src="./js/jquery.min.js"></script>
Finally write the script tag in the page, and use jQuery in the tag to set the value of the drop-down box to the default value.
<script> $(function(){ $('.users').val(2); }); </script>
Recommended tutorial: "JS Tutorial"
The above is the detailed content of How to set default value for drop-down box in JS?. For more information, please follow other related articles on the PHP Chinese website!