Ajax:当ajax响应时不加载jquery插件

时间:2013-09-11 12:18:01

标签: php javascript jquery ajax

当我从第2页获得ajax响应时,我想在page1中加载jQuery评级插件。我从数据库获取信息但未加载评级插件。

我的脚本是

temp1.php

<script type="text/javascript" src="review-project/js/jquery-1.9.0.js"></script>
<script type="text/javascript" src="review-project/js/jquery.raty.min.js"></script>
<script>
$(function () {
    $('div[id^="star"]').each(function () {
        $(this).raty({
            path: 'review-project/img',
            readOnly: true,
            score: function () {
                return $(this).attr('data-score');
            },
            half: true
        });
    });
});
    </script>
<?php $user_name="root";
$password="";
$db_name="reviewm";

mysql_connect("localhost", $user_name, $password);
mysql_select_db($db_name);?>

<?php 
echo "<h3>REVIEWS</h3>";
    $ret_reviewq    =   mysql_query("select * from reviews where hotel_id=1 AND approve='1' order by review_id desc limit 0, 2");
    @$review_count  =   mysql_num_rows($ret_reviewq);

    if($review_count<=0){echo "No Review From User";}

    while(@$ret_reviewf=mysql_fetch_array($ret_reviewq))
    {
        $rev_uid    =   $ret_reviewf['uid'];
        $rev_rating =   $ret_reviewf['rating'];
        $rev_comments   =   $ret_reviewf['comments'];

        $rname_q=mysql_query("select username from users where userid='$rev_uid'");
        $rname_f=mysql_fetch_array($rname_q);
        $rev_name=$rname_f['username'];

        @$review_output .='
        <div> 
            <div>Name: '. $rev_name.'</div>
            <div>Comments: '.$rev_comments.'</div>
            <p><div id="star" data-score="'.$rev_rating.'"></div></p>
        </div>
        ';  
    }

    echo $review_output;


$start  =   2; 
$last   =   2;
$approve =  '<div id="more"><a href=javascript:Approve(\''.$start.'-'.$last.'\')>More</a></div>';
echo $approve;
?>

<script type="text/javascript">
function Approve(str) {
    var myarr = str.split("-");
    var start = myarr[0];
    var last = myarr[1];
    if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { // code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            document.getElementById("more").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "temp2.php?last=" + last + "&start=" + start, true);
    xmlhttp.send();
}
</script>

temp2.​​php

<script type="text/javascript" src="review-project/js/jquery-1.9.0.js"></script>
<script type="text/javascript" src="review-project/js/jquery.raty.min.js"></script>
<script>
    $(function () {
    $('div[id^="star"]').each(function () {
        $(this).raty({
            path: 'review-project/img',
            readOnly: true,
            score: function () {
                return $(this).attr('data-score');
            },
            half: true
        });
    });
});
</script>
<body>

<?php

$user_name="root";
$password="";
$db_name="reviewm";

mysql_connect("localhost", $user_name, $password);
mysql_select_db($db_name);

?> 

<?php
$how_much_increase = 4;


$start  =   $_GET['start'];
$last   =   $_GET['last'];

//$start    =   0;
//$last =   3;

if($last==2){$last=$how_much_increase;}
else{$last=$how_much_increase+$last;}
$res=mysql_query("select * from reviews where hotel_id=1 AND approve='1'");
$total_review = mysql_num_rows($res);


$ret_reviewq    =   mysql_query("select * from reviews where hotel_id=1 AND approve='1' order by review_id desc limit $start, $last");
@$review_count  =   mysql_num_rows($ret_reviewq);
while(@$ret_reviewf=mysql_fetch_array($ret_reviewq))
    {
        $rev_uid    =   $ret_reviewf['uid'];
        $rev_rating =   $ret_reviewf['rating'];
        $rev_comments   =   $ret_reviewf['comments'];

        $rname_q=mysql_query("select username from users where userid='$rev_uid'");
        $rname_f=mysql_fetch_array($rname_q);
        $rev_name=$rname_f['username'];

        @$review_output .='
        <div> 
            <div>Name: '. $rev_name.'</div>
            <div>Comments: '.$rev_comments.'</div>
            <p><div id="star" data-score="'.$rev_rating.'"></div></p>
        </div>
        ';      
    }

    echo $review_output;


if($total_review <= $last)
{$approve   =   '<div  id="more">No More Reviews</div>';}
else {$approve  =   '<div id="more"><a href=javascript:Approve(\''.$start.'-'.$last.'\')>More</a></div>';}
echo $approve;
?>

1 个答案:

答案 0 :(得分:2)

请修改您的ajax成功阻止。

  if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
     document.getElementById("more").innerHTML = xmlhttp.responseText;
     $('div[id^="star"]').each(function () {
            $(this).raty({
                path: 'review-project/img',
                readOnly: true,
                score: function () {
                    return $(this).attr('data-score');
                },
                half: true
            });
        });
    }