Home > Web Front-end > JS Tutorial > body text

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

Guanhui
Release: 2020-06-13 12:00:35
Original
7053 people have browsed it

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

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

Then introduce jQuery into the page;

<script src="./js/jquery.min.js"></script>
Copy after login

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

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!

Related labels:
js
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