Home >Web Front-end >JS Tutorial >Dynamically display select drop-down list data
This time I will bring you dynamic display of select drop-down list data, what are the precautions for dynamically displaying select drop-down list data, the following is a practical case, let's take a look.
Let’s take a look at the running effect first:
The specific code is as follows:
<!DOCTYPE HTML> <html> <head> <meta charset="UTF-8"> <title>www.jb51.net jQuery动态显示表单</title> <script type="text/javascript" src="jquery-1.7.1.min.js"></script> <script type="text/javascript"> //数据集 var schools = [ { 'id': 1, 'name': '南京大学' }, { 'id': 2, 'name': '北京大学' }, { 'id': 3, 'name': '浙江大学' }, { 'id': 4, 'name': '清华大学' }, { 'id': 5, 'name': '湖南大学' }, ]; //页面加载运行,将数据集绑定select,显示默认选中学校 $(function () { bindSelect(); $('#info').text($('#schoolSelect').val()); }); //将数据集绑定select,重新选择学校后显示选中学校 bindSelect = function () { var $schoolSelect = $('#schoolSelect'); $schoolSelect.change(function () { $('#info').text($(this).val()); }); if (schools.length > 0) { for (var i = 0; i < schools.length; i++) { var item = schools[i]; if (item.id == 2) { $schoolSelect.append('<option value="' + item.id + '" selected>' + item.name + '</option>'); } else { $schoolSelect.append('<option value="' + item.id + '">' + item.name + '</option>'); } } } } </script> </head> <body> <form> <select id="schoolSelect"> </select> <label id="info"></label> </form> </body> </html>
I believe you have mastered the method after reading the case in this article. For more exciting content, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Problems in vue static resource packaging
Detailed explanation of the usage of async and await
The above is the detailed content of Dynamically display select drop-down list data. For more information, please follow other related articles on the PHP Chinese website!