根据下面代码,发送ajix请求,可以获取对应类别的数据 <select class="form-control" id="class_name" name="class_name"> <option value="">全部</option> {% for c in class_list %} <option value="{{ c }}">{{ c }}</option> {% endfor %} </select>
时间: 2023-06-16 21:05:37 浏览: 162
这段代码是一个 HTML 的下拉菜单,其中的每个选项对应一个类别。当用户选择某个类别时,可以使用 JavaScript 发送 Ajax 请求,获取对应类别的数据。具体实现可以参考以下代码:
```javascript
// 选项改变时触发
$("#class_name").change(function() {
// 获取选中的类别
var selectedClass = $(this).val();
// 发送 Ajax 请求
$.ajax({
url: "/get_data",
type: "POST",
data: { class_name: selectedClass },
success: function(data) {
// 成功获取数据后的处理
console.log(data);
},
error: function(xhr, status, error) {
// 获取数据失败后的处理
console.error(error);
}
});
});
```
在上面的代码中,我们使用 jQuery 的 `$.ajax()` 函数发送 Ajax 请求。其中,`url` 指定了请求的地址,`type` 指定了请求的类型为 POST,`data` 指定了发送的数据,这里我们发送了选中的类别名称。在请求成功后,会执行 `success` 回调函数,这里我们只是简单地将返回的数据打印出来。如果请求失败,会执行 `error` 回调函数,这里我们只是简单地将错误信息打印出来。
阅读全文
相关推荐











