2013-10-17 17 views
0

我有一個PHP的條件是這樣的:對於如何使用jQuery和PHP的if else條件

<?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"); 

    }); 

任何sugg estions?

+5

嗯,是以往任何時候都寫入頁的'unfollow'按鈕? – tymeJV

+2

它的唯一寫入條件是$ numResults> 0 – beckinho27

+4

因此,如果元素沒有寫入頁面,您會如何淡化元素? – tymeJV

回答

0
<!-- 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> 
0

你應該讓他們都在頁面上,最初淡出一個。它在這裏工作。 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"); 
}); 
0
$('#follow').click(function() 
{ 
    $("#follow").fadeOut("slow",function(){ 
    $("#follow").text("unfollow").fadeIn("slow"); 
    }); 

}); 
0

試試這個

<?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 

    });