You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JS的if语句中触发弹窗模态框?表单验证场景求助

解决手机号验证后触发AJAX并显示感谢模态框的问题

问题说明

当表单中输入的号码为10位时,将数据发送至PHP脚本,然后显示成功/感谢模态框。之前用HTML的data-bs-toggle和target绑定按钮切换模态框,但结合验证逻辑后需要在JS中主动激活模态框,目前代码存在错误,不确定模态框调用位置。

现有代码问题分析

  1. 未获取表单其他字段值:email、name、residence_type变量未从DOM元素中取值,直接使用会导致AJAX数据缺失
  2. 模态框触发逻辑错误:将按钮点击事件绑定放在if判断内部,且未在AJAX请求完成后触发,应该在AJAX成功回调中调用模态框
  3. Bootstrap模态框方法参数错误:modal("Toggle")应为小写的modal("toggle")
  4. 缺少表单重置和错误隐藏逻辑:验证通过后未重置表单,错误提示未隐藏

修正后的完整代码

HTML部分(补充模态框结构)

<section class="banner_section section_padding position-relative pb-0">
    <div class="hanging_form bg-white">
        <h2 class="colorTheme text-center fw-bold mb-4">Let's Connect</h2>
        <div class="custom_form_style">
            <form>
                <div class="form-group mb-3">
                    <input type="text" class="form-control name" id="name" placeholder="Name" />
                </div>
                <div class="form-group mb-3">
                    <input type="number" class="form-control mobile" id="mobile" placeholder="Number" />
                </div>
                <div class="form-group mb-3">
                    <input type="email" class="form-control email" id="email" placeholder="Email" />
                </div>
                <div class="form-group mb-3 send_me_updates position-relative">
                    <div class="form-check">
                        <input class="form-check-input" type="checkbox" value="" id="sendMeUpdates">
                        <label class="form-check-label" for="sendMeUpdates">
                            Send me project updates
                        </label>
                    </div>
                </div>
                <div class="form-group mb-3 flat_size">
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="checkbox" id="2bhk" name="residence_type[]" value="2bhk">
                        <label class="form-check-label" for="2bhk">2BHK</label>
                    </div>
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="checkbox" id="3bhk" name="residence_type[]" value="3bhk">
                        <label class="form-check-label" for="3bhk">3BHK</label>
                    </div>
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="checkbox" id="4bhk" name="residence_type[]" value="4bhk">
                        <label class="form-check-label" for="4bhk">4BHK</label>
                    </div>
                    <div class="form-check form-check-inline">
                        <input class="form-check-input" type="checkbox" id="villa" name="residence_type[]" value="villa">
                        <label class="form-check-label" for="villa">Villa</label>
                    </div>
                </div>
                <div class="btn-outer d-table mx-auto mt-5 position-relative">
                    <button type="button" class="btn btn-primary" id="formsubmit1" onclick="sendOTP()">Submit</button>
                    <div class="error"></div>
                </div>
            </form>
        </div>
    </div>
</section>

<!-- 感谢模态框 -->
<div class="modal fade" id="thankyouPopup2" tabindex="-1" aria-labelledby="thankyouPopupLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="thankyouPopupLabel">感谢您的提交</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                我们已收到您的信息,会尽快与您联系!
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关闭</button>
            </div>
        </div>
    </div>
</div>

JS部分(修正逻辑)

function sendOTP() {
    // 隐藏之前的错误提示
    $(".error").hide();
    
    // 获取所有表单字段值
    var number = $("#mobile").val().trim();
    var name = $("#name").val().trim();
    var email = $("#email").val().trim();
    // 获取选中的住宅类型
    var residence_type = [];
    $("input[name='residence_type[]']:checked").each(function() {
        residence_type.push($(this).val());
    });

    // 手机号验证
    if (number && number.length === 10) {
        var input = {
            "mobile": number,
            "email": email,
            "name": name,
            "residence_type": residence_type,
            "action": "just_email"
        };

        // 发送AJAX请求
        $.ajax({
            url: 'controller.php',
            type: 'POST',
            data: input,
            success: function(response) {
                console.log(response);
                // 请求成功后显示模态框
                $("#thankyouPopup2").modal("toggle");
                // 重置表单
                $("form")[0].reset();
            },
            error: function() {
                // 请求失败提示
                $(".error").html('提交失败,请稍后重试!').show();
            }
        });
    } else {
        $(".error").html('请输入有效的10位手机号码!').show();
    }
}

关键修改说明

  • 字段值获取:补充name、email和选中的residence_type取值逻辑,确保AJAX数据完整
  • 模态框触发位置:将模态框调用放在AJAX的success回调中,确保数据提交成功后再显示弹窗
  • 参数修正:将modal("Toggle")改为正确的modal("toggle")(Bootstrap方法参数为小写)
  • 用户体验优化:验证前隐藏错误提示,成功提交后重置表单,添加AJAX错误处理

内容的提问来源于stack exchange,提问作者Abhimanyu Jha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 21:39:18