如何阻止未填写表单时点击Next按钮跳转至下一页?
解决表单未填写时点击「Next」仍跳转的问题
问题根源
当前代码中,「Next」按钮被包裹在<a>标签内,点击会直接触发页面跳转,完全绕过了表单校验逻辑;同时下拉选择框未设置必填规则,输入框的required属性也因按钮类型不是提交按钮而无法生效。
修改后的完整代码
HTML(含校验逻辑)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <link rel="stylesheet" href="styleform.css"> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.0/css/all.min.css"> <script src="https://kit.fontawesome.com/f5ba79b0f3.js" crossorigin="anonymous"></script> <title>Kitchen Owner's Package</title> </head> <body> <div class="container"> <div class="title">Fill the Form</div> <div class="content"> <form id="kitchenForm" onsubmit="return handleFormSubmit()"> <div class="user-details"> <div class="input-box"> <span class="details">Kitchen Owner's Name</span> <input type="text" placeholder="Enter your name" required> </div> <div class="input-box"> <span class="details">Cloud Kitchen Name</span> <input type="text" placeholder="Enter your kitchen name" required> </div> <div class="input-box"> <span class="details">Choose a package</span> <select id="package" name="package" required> <option value="" title="select">Please choose a package from the below list:</option> <option value="onemonth">Monthly @5,000/- BDT</option> <option value="sixmonths">Half-Yearly @25,000/- BDT</option> <option value="twelvemonths">Yearly @50,000/- BDT</option> </select> </div> <div class="input-box"> <span class="details">Payment Method</span> <select id="paymentMethod" name="paymentMethod" required> <option value="" title="select">Pay Through:</option> <option value="bkash">Bkash</option> </select> </div> </div> <div class="button"> <input type="submit" value="Next"> </div> </form> </div> </div> <script> function handleFormSubmit() { const form = document.getElementById('kitchenForm'); // 调用HTML5内置校验 if (form.checkValidity()) { // 执行原发送邮件逻辑(若有实现) sendEmail(); // 校验通过后跳转 window.location.href = 'payment.html'; // 可选:重置表单 form.reset(); return false; } else { // 显示浏览器默认的校验提示 form.reportValidity(); return false; } } // 保留原sendEmail函数(需自行实现发送逻辑) function sendEmail() { // 此处编写邮件发送代码 } </script> </body> </html>
CSS(保持原代码不变)
@import url("https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap"); * { margin: 0; padding: 0; box-sizing: border-box; font-family: "Poppins", sans-serif; } body { height: 100vh; display: flex; justify-content: center; align-items: center; padding: 20px; background: linear-gradient(135deg, #f1fb40, #f5fba3); } .container { max-width: 800px; width: 400%; background-color: #fff; padding: 25px 30px; border-radius: 5px; box-shadow: 0 5px 10px rgba(0, 0, 0, 0.15); } .container .title { font-size: 25px; font-weight: 500; position: relative; } .container .title::before { content: ""; position: absolute; left: 0; bottom: 0; height: 3px; width: 30px; border-radius: 5px; background: linear-gradient(135deg, #71b7e6, #9b59b6); } .content form .user-details { display: flex; flex-wrap: wrap; justify-content: space-between; margin: 20px 0 12px 0; } form .user-details .input-box { margin-bottom: 15px; width: calc(100% / 2 - 20px); } form .input-box span.details { display: block; font-weight: 500; margin-bottom: 5px; } .user-details .input-box input { height: 40px; width: 100%; outline: none; font-size: 16px; border-radius: 5px; padding-left: 15px; border: 1px solid #ccc; border-bottom-width: 2px; transition: all 0.3s ease; } .user-details .input-box input:focus, .user-details .input-box input:valid { border-color: #9b59b6; } form .button { width: 100px; height: 45px; border-radius: 10px; margin: 35px 0; } form .button input { height: 100%; width: 100%; border-radius: 5px; border: none; color: #fff; font-size: 18px; font-weight: 500; letter-spacing: 1px; cursor: pointer; transition: all 0.3s ease; background: linear-gradient(135deg, #7469da, #c7a9d3); } form .button input:hover { background: linear-gradient(-135deg, #84b6da, #978b9c); } @media (max-width: 584px) { .container { max-width: 100%; } form .user-details .input-box { margin-bottom: 15px; width: 100%; } form .category { width: 100%; } .content form .user-details { max-height: 300px; overflow-y: scroll; } .user-details::-webkit-scrollbar { width: 5px; } } @media (max-width: 459px) { .container .content .category { flex-direction: column; } }
关键改动说明
- 重构按钮触发逻辑:移除包裹按钮的
<a>标签,将按钮改为type="submit",让它触发表单提交事件,而非直接跳转。 - 完善下拉框校验规则:给两个
<select>添加required属性,同时将默认选项的value设为空字符串,确保未选择时触发校验;修正重复的id属性(原两个下拉框都用package,改为paymentMethod避免冲突)。 - 修复表单提交逻辑:修正
onsubmit的语法错误,通过自定义函数handleFormSubmit控制校验与跳转流程。 - 添加校验逻辑:利用HTML5内置的
checkValidity()和reportValidity()方法,实现字段合法性检查,并在不合法时显示浏览器原生校验提示,阻止页面跳转。
内容的提问来源于stack exchange,提问作者Fardin Jim
相关产品推荐
相关产品推荐

