小编典典

jQuery根据ajax响应显示/隐藏Div,并将响应的requestId传递给成功的Div

ajax

根据服务调用的响应,我有以下显示/隐藏成功div的代码,此外,我需要将服务响应的请求传递给Success
Div。我如何通过requestId并显示成功div

<div id="showResponseArea" class="alert alert-success hide">
    <span>
        <strong>Success !! </strong>Your request <<requestId>> has been successfuly created !!! 
    </span>
</div>



$.ajax({
    url:
    type:
    data:
    success: function(resObj){
        $("#showResponseArea span").removeClass("hide");
        var requestId = resObj.requestId;    
    }
    error: funciton(resObj){
        alert("Some Error Occured");
    }
});

阅读 384

收藏
2020-07-26

共1个答案

小编典典

首先,您hide要从span没有该类的错误TAG 中删除该类。应该使用具有hide类的id =“
showResponseArea”从父div中移除hide类,其次,您需要<<requestId>>使用id 来包装in span。喜欢

<div id="showResponseArea" class="alert hide">
<span>
    <strong>Success !! </strong>Your request <span id="requestId"> // Request id goes here</span> has been successfuly created !!! 
</span>

然后在ajax成功函数

success: function(resObj){
    $("#showResponseArea").removeClass("hide");
    $("#showResponseArea").removeClass("alert-danger");
    $("#showResponseArea").addClass("alert-success");
    //OR $("#showResponseArea").removeClass("hide").show();

    var requestId = resObj.requestId;   
    $("#requestId").text(requestId ); 
},
error: function(err,xhr,status){
    $("#showResponseArea").removeClass("hide");
    $("#showResponseArea").removeClass("alert-success");
    $("#showResponseArea").addClass("alert-danger");
    //OR $("#showResponseArea").removeClass("hide").show();

    $("#requestId").text(xhr.responseText); 
}
2020-07-26