如何在 php 中使用 jquery if else 条件


How to use jquery with php if else conditions

我有一个这样的php条件:

<?php
    if($numResults > 0)
    { ?>
        <button id="unfollow" class="button"> unfollow </button>
<?php }
    else
    { ?>
        <button id="follow" class="button"> follow </button>
<?php } ?>

但是如果条件转到"else 语句",我想要一个 jquery 事件处理程序,以便当用户单击"关注"时,按钮淡出,"取消关注"淡入

我试过这个,但它只会淡出

"关注"按钮,而不会淡出"取消关注"按钮
$('#follow').click(function()
    {
        $("#follow").fadeOut("slow");
        $("#unfollow").fadeIn("slow");
    });

有什么建议吗?

<!-- Unfollow button -->
<button id="unfollow" 
        style="display:<?php echo ( $numResults > 0 ? 'none' : 'block' ); ?>"
        class="button">Unfollow</button>
<!-- Follow Button -->
<button id="follow" 
        style="display:<?php echo ( $numResults > 0 ? 'block' : 'none' ); ?>"
        class="button">Follow</button>

你应该把它们都放在页面上,其中一个最初淡出。它在这里工作。http://jsfiddle.net/TzkGT/

<button id="follow" class="button"> follow </button>
<button id="unfollow" class="button" style="display: none;"> unfollow </button>

$('.button').click(function(){
    $("#follow").fadeToggle("slow");
    $("#unfollow").fadeToggle("slow");
});
$('#follow').click(function()
{
    $("#follow").fadeOut("slow",function(){
    $("#follow").text("unfollow").fadeIn("slow");
    });
});

试试这个

<?php
    if($numResults > 0)
    { ?>
        <button id="unfollow" class="button"> unfollow </button>
<?php }
    else
    { ?>
        <button id="follow" class="button"> follow </button>
        <button id="unfollow" class="button"> unfollow </button>
        <style>
            #unfollow { display:none;}
        </style>
<?php } ?>

Js

$('#follow').click(function()
    {
        $("#follow").fadeOut("slow");
        $("#unfollow").css("display","block");
        $("#unfollow").fadeIn("slow"); // this may not require if you don't want effect
    });