0

我正在为一个网站开发一个“订阅模块”,除了
Ajax 部分,一切都很好。

当我一次单击“订阅”时,按钮变为“取消订阅”,但如果我再次单击“取消订阅”按钮而不刷新页面,按钮将变为“订阅”,而不是再次“取消订阅”。

就像我点击了两次一样。

这是我从“chrome -> network tab”看到的

engine/ajax/mywatch.php?subscribe_id=3385&action=plus&skin=Default
engine/ajax/mywatch.php?subscribe_id=3385&action=minus&skin=Default

html

<a id="subscript-id-3385" onclick="doMywatch('3385', 'plus'); return false;" >subscribe</a>

javascript

<script>
    function doMywatch(a,b){ShowLoading("");$.get(root+"engine/ajax/mywatch.php",{subscribe_id:a,action:b,skin:skin},function(b){HideLoading("");$("#subscript-id-"+a).html(b)});return!1}
</script> 

php

    if( $_GET['action'] == "plus" ) {

    echo <<<HTML
    <script>
            alert('plus ');
    </script>
    HTML;
            $row = $db->super_query("SELECT subscriptions FROM " . PREFIX . "_mywatch WHERE user_id={$member_id['user_id']}" );
            $list = explode(",", $row['subscriptions']);

            foreach ( $list as $daten ) {
                    if( $daten == $id ) $achecker = "stop";
            }


            $error = "";

            if( $achecker != "stop" ){

                    $list[] = $id;
                    $subsc = $db->safesql(implode( ",", $list ));
                    //$subsc = implode( ",", $list );
                    if( $row['subscriptions'] == "" ) $subsc = $id;
                    $db->query( "UPDATE " . PREFIX . "_mywatch set subscriptions='$subsc' where user_id = '{$member_id['user_id']}'" );
            }
                    $buffer = "<a onclick=\"doMywatch('" . $id . "', 'minus'); \" title=\"" . $lang['news_minfav'] . "\">unsubscribe</a>";

} elseif( $_GET['action'] == "minus" ) {

echo <<<HTML
    <script>
                alert('minus ');
        </script>
HTML;

        $error = "";

        $row = $db->super_query("SELECT subscriptions FROM " . PREFIX . "_mywatch WHERE user_id={$member_id['user_id']}" );
        $list = explode(",", $row['subscriptions']);

        foreach ( $list as $daten ) {
                if( $daten == $id ) $achecker = "stop";
        }

        if( $achecker == "stop" ){
                $list = array_merge(array_diff($list, array($id)));
                $subsc = $db->safesql(implode( ",", $list ));
                //$subsc = implode( ",", $list );
                $db->query( "UPDATE " . PREFIX . "_mywatch set subscriptions='$subsc' where user_id = '{$member_id['user_id']}'" );
        }
                $buffer = "<a onclick=\"doMywatch('" . $id . "', 'plus'); \" title=\"" . $lang['news_minfav'] . "\">Подписатся</a>";
} else
        die( "error" );

$db->close();

@header( "Content-type: text/html; charset=" . $config['charset'] );
echo $buffer;
4

1 回答 1

1

如果我是对的,第一次单击订阅时,您将进入elseif( $_GET['action'] == "plus" ),然后您的输出将<a onclick=......>Unsubscibe</a>被放入您的上一个<a id="subscript-id-3385">...</a>,因此您将在同一个元素中同时拥有订阅和取消订阅的点击事件,所以这可以导致一些奇怪的行为。我建议您将链接包装在 div 中并仅更新该 div。

于 2013-06-11T13:20:11.667 回答