Home >Web Front-end >JS Tutorial >How to set default value for drop-down box in JS?

How to set default value for drop-down box in JS?

Guanhui
GuanhuiOriginal
2020-06-13 12:00:357096browse

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(){

            $(&#39;.users&#39;).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!

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