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

AJAX success回调无法调用函数及多数jQuery方法问题求助

问题解决步骤

1. 修复核心语法错误

代码最后一行的)}是语法错误,需改为});。这个错误会导致整个$(document).ready代码块无法正确执行,进而引发后续函数调用、jQuery方法失效的问题。

2. 修正HTML字符串转义问题

你在拼接HTML字符串时使用了HTML实体(如<、"),这会导致页面显示实体字符而非渲染成HTML标签。直接使用原生HTML符号即可:

function errMsg(code, msg) {
    const eCode = '<b>E-NS: ' + code + ' </b><br>' + msg;
    const eMsg = '<span class="err" style="color: red;">' + eCode + '</span>';
    
    $('.notify').empty().html(eMsg);
}

3. 简化错误处理逻辑

success回调里重复拼接HTML的代码完全可以通过调用errMsg函数替代,避免冗余。同时原代码中eMsg字符串存在引号未闭合的问题,修正后直接复用函数即可:

success:function(response){
    if(response.status === true){
        $('#form-1').hide();
        $('#form-2').show();
    } else {
        console.log(response.status);
        console.log('Response = ' + response.code + response.error);
        // 直接调用errMsg处理错误信息
        errMsg(response.code, response.error);
    }
}

4. 确认DOM元素存在

确保页面中存在带有.notify类的元素,比如:

<div class="notify"></div>

该元素需要在页面加载时就存在,否则jQuery无法选中并修改它。

修正后的完整代码

function errMsg(code, msg) {
    const eCode = '<b>E-NS: ' + code + ' </b><br>' + msg;
    const eMsg = '<span class="err" style="color: red;">' + eCode + '</span>';
    
    $('.notify').empty().html(eMsg);
}

$(document).ready(function() {
    $('#next-1').click(function(e) {
        e.preventDefault();
        $.ajax({
            url:'../data.php',
            method: 'POST',
            data: $('#form-1').serializeArray(),
            dataType: 'JSON',
            success:function(response){
                if(response.status === true){
                    $('#form-1').hide();
                    $('#form-2').show();
                } else {
                    console.log(response.status);
                    console.log('Response = ' + response.code + response.error);
                    errMsg(response.code, response.error);
                }
            }
        });
    });
});

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 08:03:33