0

我有一个通过 ajax 提交的表单:

 $(function() {
 $(".submit").click(function() {
var listid = $("#listid").val();
var itemid = "<?=$id?>";
var userid = "<?=$_SESSION['user_id']?>"; 

 var dataString = 'listid=' + listid + '&userid=' + userid + '&itemid=' + itemid;


 $.ajax({
 type: "POST",
 url: "join.php",
 data: dataString,
 success: function(){
 $('#content_error').fadeIn(200).show();
 }
 });

 return false;
 });
 });

这完美地工作。ajax函数调用join.php:

  <? session_start();

require_once("include/database.php");
$Db = new Database();

 if($_POST['listid'] && $_POST['itemid'] && $_POST['userid']) {

            $list_id = $_POST['listid'];
            $user_id = $_POST['userid'];
            $item_id = $_POST['itemid'];

            $sql = "SELECT *
                    FROM items_list
                    WHERE list_id = '".$list_id."'
                    AND user_id = '".$user_id."'
                    AND item_id = '".$item_id."'
                    ORDER BY item_id DESC LIMIT 1";
            $result = $Db->sQuery($sql);
            $row = mysql_fetch_array($result);

            if(mysql_num_rows($result) > 0){

                $error = "You've already saved this";   

            }else{

            $sql = "INSERT INTO items_list (list_id, user_id, item_id) 
                    VALUES('$list_id', '$user_id', '$item_id')";
            $Db->uidQuery($sql);

            $sql = "SELECT count(item_id) as itemm
                    FROM items_list 
                    WHERE item_id = '".$item_id."'
                    GROUP BY item_id";          
            $result = $Db->sQuery($sql);
            $Db->closeConnection();
            $tel2 = mysql_fetch_array($result);

            ?>
                <span class="bubble_itemm"><?=$tel2['itemm']?></span>


            <?
                $success = "Sucessfully";

            }
        }

?>

成功后,我使用类bubble_itemm 调用span。这个跨度也存在于主页中。我希望“新”跨度用淡入淡出替换主页中的“旧”跨度。

我该怎么做呢?

4

3 回答 3

1
$(function() {
 $(".submit").click(function() {
var listid = $("#listid").val();
var itemid = "<?=$id?>";
var userid = "<?=$_SESSION['user_id']?>"; 

 var dataString = 'listid=' + listid + '&userid=' + userid + '&itemid=' + itemid;


 $.ajax({
 type: "POST",
 url: "join.php",
 data: dataString,
 success: function(data){
 $('#content_error').fadeIn(200).show();
 $('.bubble_itemm').text(data);
 }
 });

 return false;
 });
 });

$('.bubble_itemm').text(data); 添加此代码并从 join.php 中删除 span 标签,这只是在您的 span 中回显您想要的文本。

<? session_start();

require_once("include/database.php");
$Db = new Database();

 if($_POST['listid'] && $_POST['itemid'] && $_POST['userid']) {

            $list_id = $_POST['listid'];
            $user_id = $_POST['userid'];
            $item_id = $_POST['itemid'];

            $sql = "SELECT *
                    FROM items_list
                    WHERE list_id = '".$list_id."'
                    AND user_id = '".$user_id."'
                    AND item_id = '".$item_id."'
                    ORDER BY item_id DESC LIMIT 1";
            $result = $Db->sQuery($sql);
            $row = mysql_fetch_array($result);

            if(mysql_num_rows($result) > 0){

                $error = "You've already saved this";   

            }else{

            $sql = "INSERT INTO items_list (list_id, user_id, item_id) 
                    VALUES('$list_id', '$user_id', '$item_id')";
            $Db->uidQuery($sql);

            $sql = "SELECT count(item_id) as itemm
                    FROM items_list 
                    WHERE item_id = '".$item_id."'
                    GROUP BY item_id";          
            $result = $Db->sQuery($sql);
            $Db->closeConnection();
            $tel2 = mysql_fetch_array($result);

            ?>
                <? echo $tel2['itemm'];?>


            <?
                $success = "Sucessfully";

            }
        }

如果你想淡入效果使用这个代码

$('.bubble_itemm').fadeOut(1000,function(){ $(this).text(data).fadeIn(1000); });
于 2012-08-16T09:58:17.763 回答
0

表格页面:

<script type="text/javascript">
$(function() {
    $(".submit").click(function() {
        var listid = $("#listid").val();
        var itemid = "<?=$id?>";
        var userid = "<?=$_SESSION['user_id']?>"; 

        var dataString = 'listid=' + listid + '&userid=' + userid + '&itemid=' + itemid;


        $.ajax({
            type: "POST",
            url: "join.php",
            data: dataString,
        }).done(function( result ) {
            myresult(result);
        });

        return false;
    });
});

function myresult(result) {
    var result_lines = result.split("<splitter>");    
    if (result_lines[0] == '1') { 
        $('.bubble_itemm').html('<span class="error">' + result_lines[1] + '</span>').fadeIn(500);
    } else if (result_lines[0] == '2') { 
        $('.bubble_itemm').html('<span class="success">' + result_lines[1] + '</span>').fadeIn(500);
    }
    return true;   
}

</script>

join.php 脚本:

<?php 

require_once("include/database.php");
$Db = new Database();

if($_POST['listid'] && $_POST['itemid'] && $_POST['userid']) {

    $list_id = $_POST['listid'];
    $user_id = $_POST['userid'];
    $item_id = $_POST['itemid'];

    $sql = "SELECT *
            FROM items_list
            WHERE list_id = '".$list_id."'
            AND user_id = '".$user_id."'
            AND item_id = '".$item_id."'
            ORDER BY item_id DESC LIMIT 1";
    $result = $Db->sQuery($sql);
    $row = mysql_fetch_array($result);

    if(mysql_num_rows($result) > 0){
        echo "1<splitter>"; // here to identify message status
        echo "You've already saved this"; // return message

    }else{

        $sql = "INSERT INTO items_list (list_id, user_id, item_id) 
                VALUES('$list_id', '$user_id', '$item_id')";
        $Db->uidQuery($sql);

        $sql = "SELECT count(item_id) as itemm
                FROM items_list 
                WHERE item_id = '".$item_id."'
                GROUP BY item_id";          
        $result = $Db->sQuery($sql);
        $Db->closeConnection();
        $tel2 = mysql_fetch_array($result);

        echo "2<splitter>"; // here to identify message status
        echo $tel2['itemm']; // return message
        echo "Sucessfully"; // return message

    }
}
?>
于 2012-08-16T09:44:41.157 回答
0

如果您在主页中为目标跨度分配了一个 id,那肯定会有所帮助。对一类跨度做某事会影响该类的所有跨度。也许这是所希望的。

您可以将任意数量的函数添加到 AJAX 请求的成功部分。您应该研究的函数是fadeOutremovefadeIn。这些应该使用回调按顺序调用。例如

$('#old_span_id').fadeOut('slow', function() {
    $(this).remove();
    $(new_span).insertAfter('#where_want_it_to_go');
    $(new_span).fadeIn();
});

无论如何,我需要看更多的 HTML 才能给你一个更完整的答案。

于 2012-08-16T09:49:26.933 回答