document.addEventListener('DOMContentLoaded', function() { const links = document.querySelectorAll('.nav-pills .nav-link'); // Get the current page path const currentPath = window.location.pathname; links.forEach(link => { // Compare the link's href with the current path if (link.href.endsWith(currentPath)) { link.classList.add('active'); } else { link.classList.remove('active'); } }); }); function showModal(message) { // 设置 Modal 中的消息 document.getElementById('modalMessage').innerText = message; // 显示 Modal const responseModal = new bootstrap.Modal(document.getElementById('responseModal')); responseModal.show(); } //动态填充select控件 function set_select_data(select_ele_id,datas){ const select_Ele = document.getElementById(select_ele_id); //清空老数据 select_Ele.innerHTML = ''; //添加列表 datas.forEach(option => { const optionElement = document.createElement('option'); optionElement.textContent = option; select_Ele.appendChild(optionElement); }); } //设定选项选中状态---下拉框 function set_select_selct(select_ele_id,option_str){ let bfind = false; const select_Ele = document.getElementById(select_ele_id); for(let i=0;i< select_Ele.options.length;i++){ if(select_Ele.options[i].value === option_str){ select_Ele.options[i].selected = true; bfind = true; break; } } return bfind; } //设定单选按钮选中状态 ----单选按钮 function set_radio_selection(groupName, targetValue) { // 获取所有同名的 radio 按钮 const radios = document.getElementsByName(groupName); // 遍历 radio 按钮 for (const radio of radios) { if (radio.value === targetValue) { radio.checked = true; // 选中匹配项 return true; // 返回成功 } } console.error(`未找到值为 ${targetValue} 的 radio 按钮`); return false; // 未找到匹配项 } // 获取选中值--返回的value值 ----单选按钮 function get_radio_value(groupName) { return document.querySelector(`input[name="${groupName}"]:checked`)?.value; } //将输入框内容转换为单精度型,若转换失败则返回0 -- 若需要整型,parseInt function getInputValueAsFloat(id) { var value = document.getElementById(id).value; return value ? parseFloat(value) : 0; } //正则匹配IP是否合法 return true,false function isValidIP(ip) { const ipRegex = /^(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.(25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$/; return ipRegex.test(ip); } //post数据 async function postJSON(url, payload) { const res = await fetch(url, { method: 'POST', headers: { 'Content-Type':'application/json' }, body: JSON.stringify(payload) }); if (!res.ok) { const errorData = await res.json(); throw new Error(errorData.error || `HTTP错误 ${res.status}`); } return res.json(); } /* ---------- 简易 Toast ---------- */ function showToast(msg, type='info') { const toastEl = document.createElement('div'); toastEl.className = `toast align-items-center text-white bg-${type} border-0 position-fixed bottom-0 end-0 m-3`; toastEl.role = 'alert'; toastEl.innerHTML = `