jQuery 机票城市选择表单代码通常用于创建一个用户友好的界面,允许用户从预定义的城市列表中选择出发地和目的地。以下是一个简单的示例代码,展示了如何使用 jQuery 创建一个机票城市选择表单:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>机票城市选择表单</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<style>
.city-select {
margin: 10px 0;
}
.city-select label {
margin-right: 10px;
}
.city-select select {
padding: 5px;
}
</style>
</head>
<body>
<div class="city-select">
<label for="departure">出发地:</label>
<select id="departure">
<!-- 城市选项将通过 jQuery 动态添加 -->
</select>
</div>
<div class="city-select">
<label for="destination">目的地:</label>
<select id="destination">
<!-- 城市选项将通过 jQuery 动态添加 -->
</select>
</div>
<script>
$(document).ready(function() {
var cities = [
{ name: '北京', code: 'BJ' },
{ name: '上海', code: 'SH' },
{ name: '广州', code: 'GZ' },
{ name: '深圳', code: 'SZ' },
{ name: '成都', code: 'CD' }
];
function populateCities(selectElementId) {
var $select = $('#' + selectElementId);
$.each(cities, function(index, city) {
$select.append($('<option>', {
value: city.code,
text : city.name
}));
});
}
populateCities('departure');
populateCities('destination');
});
</script>
</body>
</html>
通过上述代码和解释,你应该能够创建一个基本的机票城市选择表单,并了解其背后的基础概念、优势、类型和应用场景。
领取专属 10元无门槛券
手把手带您无忧上云