ajax成功后,使用td将文本添加到span标签

时间:2018-07-26 11:06:39

标签: html ajax function onclick

我想在ajax成功之后将文本添加到td中的span标签中

function Recommended(parameters){
    var split_para = parameters.split(',');
    var batchID = split_para[0];
    var moduleID = split_para[1];
    var studID = split_para[2];
    var sessionID = split_para[3];
    $.ajax({
        type: 'post',
        async: false,
        url: "fn_review_submit.php",
        data:{
            batchid1: batchID,
            moduleid1: moduleID,
            STD_ID: studID,
            sessionid1: sessionID
            },
success: function(data) {
    alert(data);

 if(data == 1) {
     alert('Record update successfully');
     $("#submittername").text('Recommend');
 }else{
    //alert(Error);
    $("#submittername").text('-');
 }

    }
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<td><span id="submittername"></span></td>

问题是,当数据到来等于1,但span标签没有通过建议更新span时,我也使用了$("#submittername").html('Recommend');,但仍然没有更新span。在ajax成功之后,还有其他方法可以使用添加文本来更新span标签吗?

1 个答案:

答案 0 :(得分:1)

在列出数据的同时,定义其主键来像这样跨标签

<span id="submittername_<?php $primaryid; ?>">sometext</span>

,并使用您的参数发送此主要ID,以便在jquery之后

function Recommended(parameters){
    var split_para = parameters.split(',');
    var batchID = split_para[0];
    var moduleID = split_para[1];
    var studID = split_para[2];
    var sessionID = split_para[3];
// id given below
var primaryid = split_para[4];

    $.ajax({
        type: 'post',
        async: false,
        url: "fn_review_submit.php",
        data:{
            batchid1: batchID,
            moduleid1: moduleID,
            STD_ID: studID,
            sessionid1: sessionID
            },
success: function(data) {
    alert(data);

 if(data == 1) {
     alert('Record update successfully');
     $("#submittername_"+primaryid ).empty();
 $("#submittername_"+primaryid ).append('Recommend');


 }else{
    //alert(Error);
    $("#submittername_"+primaryid ).empty();
 $("#submittername_"+primaryid ).append('-');
 }

    }
});
}