要在表单提交后显示由 glob
函数生成的下拉菜单过滤器选定值,通常涉及到以下几个步骤:
glob
函数的结果动态生成选项,适用于文件选择等场景。glob
函数获取文件列表。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Form Submission</title>
</head>
<body>
<form action="/submit" method="post">
<select name="fileSelector" id="fileSelector">
<!-- Options will be populated by JavaScript -->
</select>
<button type="submit">Submit</button>
</form>
<div id="selectedValue"></div>
<script>
document.addEventListener('DOMContentLoaded', function() {
fetch('/getFiles')
.then(response => response.json())
.then(files => {
const select = document.getElementById('fileSelector');
files.forEach(file => {
const option = document.createElement('option');
option.value = file;
option.textContent = file;
select.appendChild(option);
});
});
document.querySelector('form').addEventListener('submit', function(event) {
event.preventDefault();
const formData = new FormData(this);
fetch('/submit', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
document.getElementById('selectedValue').textContent = `Selected: ${data.selected}`;
});
});
});
</script>
</body>
</html>
from flask import Flask, request, jsonify
import glob
app = Flask(__name__)
@app.route('/getFiles', methods=['GET'])
def get_files():
files = glob.glob('path/to/files/*.txt') # Adjust the pattern as needed
return jsonify(files)
@app.route('/submit', methods=['POST'])
def submit():
selected_file = request.form.get('fileSelector')
return jsonify({'selected': selected_file})
if __name__ == '__main__':
app.run(debug=True)
fetch
API 从服务器获取文件列表并填充下拉菜单。fetch
发送数据到服务器,并显示选定值。/getFiles
路由使用 glob
函数获取文件列表并返回。/submit
路由接收表单数据,并返回选定的文件名。glob
函数的路径不正确或服务器端没有正确处理请求。glob
函数的路径模式,确保服务器端正确响应 /getFiles
请求。通过以上步骤和示例代码,可以实现表单提交后显示由 glob
函数生成的下拉菜单过滤器选定值。
领取专属 10元无门槛券
手把手带您无忧上云