求助:如何为多折叠区块添加独立运行的表单(附代码)
实现折叠区块的独立表单(可扩展)
以下是修改后的代码,已完成logo和mascota两个独立可运行的表单,代码结构清晰,方便你复制扩展第三个web区块的表单:
关键修改点
- 每个表单使用唯一ID(
form-logo、form-mascota),避免全局ID冲突 - 修正了原代码中表单内radio与外层区块ID重复的问题(比如将radio的id改为
service-logo) - 编写了通用的表单提交处理函数,每个表单绑定独立的提交逻辑,无需重复写JS代码
- 保留了你原有的折叠显示/隐藏逻辑,表单仅在对应区块展开时显示
<!DOCTYPE html> <html lang="es"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Formularios Desplegables</title> <style> /*======COMIENZO DEL FORMULARIO======*/ *{ margin: 0; padding: 0; text-decoration: none; font-family: "Montserrat", sans-serif; } body{ min-height: 100vh; background-image: linear-gradient(120deg, #3498db, #8e44ad); padding: 20px; } .section-container { margin-bottom: 20px; } .formulario{ width: 360px; background: #f1f1f1; padding: 80px 40px; border-radius: 10px; box-shadow: 2px 2px 10px 5px rgba(0, 0, 0, 0.2); margin: 20px auto 0; } .formulario h1{ text-align: center; margin-bottom: 60px; color: #333; font-weight: 600; } .box-input{ border-bottom: 2px solid #adadad; position: relative; margin: 30px 0; } .box-input input{ font-size: 16px; color: #333; border: none; width: 100%; outline: none; background: none; padding: 0 5px; height: 40px; } .box-input span::before { content: attr(data-placeholder); position: absolute; top: 50%; left: 5px; color: #adadad; transform: translateY(-50%); z-index: -1; transition: .5s; } .box-input label { position: absolute; top: 50%; left: 5px; color: #adadad; transform: translateY(-50%); z-index: -1; transition: .5s; } .box-input span::after{ content: ''; position: absolute; width: 0%; height: 2px; background: linear-gradient(120deg, #3498db, #8e44ad); transition: .5s; } .focus + span::before{ top: -5px; } .focus + span::after{ width: 100%; } .boton{ display: block; width: 100%; height: 50px; background-color: #48bb78; border: solid 2px #48bb78; color: #fff; border-radius: 5px; font-weight: 600; font-size: 18px; box-shadow: 2px 2px 5px 2px rgba(0, 0, 0, 0.1); cursor: pointer; } .boton:hover{ background-color: #2F855A; border: solid 2px #2F855A; transition: .5s; } /*======FIN DEL FORMULARIO======*/ #hide-logo, #hide-mascota, #hide-web { display: none; } .hide-content { display: none; } .styled-button { background-color: red; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; } .show-button { background-color: #3498db; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; } </style> </head> <body> <div> <div class="section-container"><!--LOGOS--> <button id="show-logo" class="show-button" onclick="showContent('logo')">Mostrar Formulario Logo</button> <button id="hide-logo" class="styled-button" onclick="hideContent('logo')">Ocultar Formulario Logo</button> <div id="logo" class="hide-content"> <form id="form-logo" class="formulario"> <h1>Servicio de Logos</h1> <div class="box-input"> <input name="nombre" id="nombre-logo" type="text" required> <span data-placeholder="Escriba su Nombre"></span> </div> <div class="box-input"> <input name="apellidos" id="apellidos-logo" type="text" required> <span data-placeholder="Escriba sus Apellidos"></span> </div> <div class="box-input"> <input name="email" id="email-logo" type="email" required> <span data-placeholder="Escriba su Correo Electrónico"></span> </div> <div class="box-input"> <label for="service-logo">Solicito Diseño de Logo</label> <input name="service" id="service-logo" type="radio" value="Diseño de Logo" required> </div> <button type="submit" class="boton"><i class="fab fa-whatsapp"></i> Enviar WhatsApp</button> </form> </div> </div> <div class="section-container"><!--MASCOTAS--> <button id="show-mascota" class="show-button" onclick="showContent('mascota')">Mostrar Formulario Mascota</button> <button id="hide-mascota" class="styled-button" onclick="hideContent('mascota')">Ocultar Formulario Mascota</button> <div id="mascota" class="hide-content"> <form id="form-mascota" class="formulario"> <h1>Servicio de Mascotas</h1> <div class="box-input"> <input name="nombre" id="nombre-mascota" type="text" required> <span data-placeholder="Escriba su Nombre"></span> </div> <div class="box-input"> <input name="apellidos" id="apellidos-mascota" type="text" required> <span data-placeholder="Escriba sus Apellidos"></span> </div> <div class="box-input"> <input name="email" id="email-mascota" type="email" required> <span data-placeholder="Escriba su Correo Electrónico"></span> </div> <div class="box-input"> <label for="service-mascota">Solicito Diseño de Mascota</label> <input name="service" id="service-mascota" type="radio" value="Diseño de Mascota" required> </div> <button type="submit" class="boton"><i class="fab fa-whatsapp"></i> Enviar WhatsApp</button> </form> </div> </div> <div class="section-container"><!--WEB--> <button id="show-web" class="show-button" onclick="showContent('web')">Mostrar Formulario Web</button> <button id="hide-web" class="styled-button" onclick="hideContent('web')">Ocultar Formulario Web</button> <div id="web" class="hide-content"> <!-- 复制上面的表单结构,修改ID为form-web,对应字段ID加-web后缀即可 --> <p>复制logo/mascota的表单结构到此处,修改对应ID即可扩展</p> </div> </div> </div> <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script> <script type="text/javascript"> $(".box-input input").on("focus", function(){ $(this).addClass("focus"); }); $(".box-input input").on("blur", function(){ if($(this).val() == ""){ $(this).removeClass("focus"); } }); </script> <script src="https://kit.fontawesome.com/a076d05399.js"></script> <script> function isMobile() { if (sessionStorage.desktop) return false; else if (localStorage.mobile) return true; var mobile = ['iphone', 'ipad', 'android', 'blackberry', 'nokia', 'opera mini', 'windows mobile', 'windows phone', 'iemobile']; for (var i in mobile) if (navigator.userAgent.toLowerCase().indexOf(mobile[i].toLowerCase()) > 0) return true; return false; } const telefono = '584129526003'; const urlDesktop = 'https://web.whatsapp.com/'; const urlMobile = 'whatsapp://'; // 通用表单提交处理函数 function handleFormSubmit(formId) { const form = document.getElementById(formId); form.addEventListener('submit', (event) => { event.preventDefault(); const button = form.querySelector('.boton'); button.innerHTML = '<i class="fas fa-circle-notch fa-spin"></i>'; button.disabled = true; setTimeout(() => { // 获取当前表单的字段值 const nombre = form.querySelector('[name="nombre"]').value; const apellidos = form.querySelector('[name="apellidos"]').value; const email = form.querySelector('[name="email"]').value; const service = form.querySelector('[name="service"]:checked').value; // 构造WhatsApp消息 let mensaje = `send?phone=${telefono}&text=*_SERVICIO SOLICITADO_*%0A*Nombre:*%0A${nombre}%0A*Apellidos:*%0A${apellidos}%0A*Correo:*%0A${email}%0A*Servicio:*%0A${service}`; // 打开WhatsApp if(isMobile()) { window.open(urlMobile + mensaje, '_blank'); } else { window.open(urlDesktop + mensaje, '_blank'); } // 恢复按钮状态 button.innerHTML = '<i class="fab fa-whatsapp"></i> Enviar WhatsApp'; button.disabled = false; }, 3000); }); } // 为每个表单绑定处理函数 handleFormSubmit('form-logo'); handleFormSubmit('form-mascota'); // SECCIONES DESPLEGABLES // 初始显示logo表单 document.getElementById('show-logo').style.display = 'none'; document.getElementById('hide-logo').style.display = 'block'; document.getElementById('logo').style.display = 'block'; function showContent(contentId) { document.getElementById('show-' + contentId).style.display = 'none'; document.getElementById('hide-' + contentId).style.display = 'block'; document.getElementById(contentId).style.display = 'block'; // 隐藏其他区块 const allSections = ['logo', 'mascota', 'web']; allSections.forEach(id => { if(id !== contentId) { document.getElementById('show-' + id).style.display = 'block'; document.getElementById('hide-' + id).style.display = 'none'; document.getElementById(id).style.display = 'none'; } }); } function hideContent(contentId) { document.getElementById('show-' + contentId).style.display = 'block'; document.getElementById('hide-' + contentId).style.display = 'none'; document.getElementById(contentId).style.display = 'none'; } </script> </body> </html>
扩展说明
要添加第三个web表单,只需:
- 复制logo区块的表单代码到web的
div#web中 - 将表单ID改为
form-web - 将表单内的字段ID后缀改为
-web(比如nombre-web) - 在JS中添加一行
handleFormSubmit('form-web');
内容的提问来源于stack exchange,提问作者Fabio Chirino Spades
相关产品推荐
相关产品推荐

