简体   繁体   English

在ajax调用之后将HTML附加到特定的DOM

[英]Appending HTML to specific DOM after ajax call

In my php page, I've three lists ul#line, ul#comment, ul#vote. 在我的php页面中,我列出了ul#line,ul#comment,ul#vote的三个列表。

I'm going to make an ajax call, something like 我要拨打一个ajax电话,类似

           $.ajax({
        type: "POST",
        url: "ajax.php",
        data: dataString,

        cache: false,
        success: function(html){
            $("ul#line").append(html);
            $("ul#lineli:last").fadeIn("slow");
    }

Ajax.php is something like Ajax.php就像

if(isset($line)){
    echo $line;
} elseif(isset($comment)){
    echo $comment;
} elseif (isset($vote)){
    echo $vote;
} else {
   //do nothing;
}

What I want is, if the echoed out HTML is $line, it'll be appended to ul#line; 我想要的是,如果回显的HTML是$ line,它将附加到ul#line;上。 if it is $comment, it'll be appended to ul#comment; 如果是$ comment,它将附加到ul#comment;中。 if it is $vote, it'll be appended to ul#vote. 如果是$ vote,则将其附加到ul#vote。

The current ajax call appends only at ul#line. 当前的ajax调用仅在ul#line处追加。

What do I need to change to achieve this?? 我需要更改以实现此目标?

I would pass back the information as JSON. 我会将信息作为JSON传回。 Have something like: 有类似的东西:

{updateList : nameOfList, output: $line/$output/$vote }

Then on success you could do something like 然后,如果成功,您可以做类似的事情

$('#'+html.updateList).append(html.output);

You have to make sure to let jQuery know that you are sending and to accept json as the type back though. 您必须确保让jQuery知道您正在发送并接受json作为返回类型。

php PHP

class DataObject
{
    public $Type;
    public $Text;
}
    $json=new DataObject();

if(isset($line)){
    $json->Type="line";
    $json->Text=$line;
    return json_encode($json);
} elseif(isset($comment)){
    $json->Type="comment";
    $json->Text=$comment;
    return json_encode($json);
} elseif (isset($vote)){
    $json->Type="vote";
    $json->Text=$vote;
    return json_encode($json);
} else {
   //do nothing;
}

javascript JavaScript的

    $.ajax({
    type: "POST",
    url: "ajax.php",
    dataType: 'json',  //add data type
    data: dataString,    
    cache: false,
    success: function(data){
        $("ul#"+data.type).append(data.text);
        $("ul#"+data.type+"li:last").fadeIn("slow");
     }

You need some way to differentiate the values of $line and $comment . 您需要某种方法来区分$line$comment的值。

I'd suggest sending back JSON from your PHP script: 我建议从您的PHP脚本发送回JSON:

if(isset($line)){
    echo '{"line" : ' . json_encode($line) . '}';
} elseif(isset($comment)){
    echo '{"comment" : ' . json_encode($comment) . '}';
} elseif (isset($vote)){
    echo '{"vote" : ' . json_encode($vote) . '}';
} else {
   //do nothing;
}

Note: PHP isn't my strongest language so there might be a better way to generate the JSON response 注意: PHP不是我最强的语言,因此可能会有更好的方法来生成JSON响应

success: function(data){
           if(data.line) {
              $("ul#line").append(html);
              $("ul#lineli:last").fadeIn("slow");
           }
           else if(data.comment) {
              $("ul#comment").append(html);
              $("ul#commentli:last").fadeIn("slow");
           }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM