$(function () {
$(".form-select").change(function (event) {
const selectedOptionValue = this.value; // 获取选中的Option值
// 使用 .closest() 向上找到最近的<tr>,然后使用 .find() 在该<tr>内向下找到<td>.Rechnernummer下的<a>标签,并获取其文本内容
const hostname = $(this).closest('tr').find('td.Rechnernummer a').text();
alert("选中的值: " + selectedOptionValue);
alert("对应的主机名: " + hostname);
// 在此处可以构建AJAX请求,将数据发送到Django后端
// 例如:
// $.ajax({
// url: "/your-django-endpoint/", // 替换为你的Django视图URL
// type: "POST",
// data: {
// option_value: selectedOptionValue,
// hostname: hostname,
// csrfmiddlewaretoken: '{{ csrf_token }}' // 如果是Django,需要CSRF令牌
// },
// success: function(response) {
// console.log("数据发送成功:", response);
// },
// error: function(xhr, status, error) {
// console.error("数据发送失败:", error);
// }
// });
// 如果需要,可以重置select的选中状态,但这通常不是change事件的默认行为,
// 且可能会导致用户体验问题,除非有特殊需求。
// for(var i = 0; i < this.options.length; i++){
// this.options[i].selected = false;
// }
});
});登录后复制