2012-03-01 43 views
0

所以我有一個包含多個發佈和取消發佈按鈕的表。ajax的jQuery Parent

簡單按鈕:

<button id="publish" 
    <?php 
     if ($singleBlog->isPublished()) 
      echo ' class="green small"'; 
     else 
      echo ' class="green red small"'; 
    ?> 
    value="<?php echo $singleBlog->getBlogId(); ?>">publish</button> 

添加的按鈕。

然後我做ajax調用,返回一些東西,並更改切換按鈕類和名稱。 它曾經工作之前,我添加了ajax,但我似乎無法使它與ajax調用工作。

我懷疑這是因爲在ajax調用中引用它,我似乎無法弄清楚如何得到它的父母!

任何幫助將不勝感激這一個。

<script> 
     $(document).ready(function() { 
      $("button#publish").click(function() { 
      //alert($(this).attr("value")); 
      var id = $(this).attr("value"); 
      $.ajax({ 
       type: "POST", 
       url: "ajax/blogPublishUnpublish.php", 
       data: "id="+ id , 
       success:function(result){ 
        var button = this; 
        if (result == '0' || result == '1'){ 
         alert("in"); 
         $(button).toggleClass("red"); 

         if($(this).is('.green')) 
          $(this).text('publish'); 

         if($(this).is('.red')) 
          $(this).text('unpublish'); 
         alert("done"); 
        } 

       } 
      }); 
      // alert(index); 


     }); 
     }); 
     </script> 

回答

0

我會再次選擇按鈕,或者如果您擔心切換ID,請創建一個變量。

$(document).ready(function() { 
     $("button#publish").click(function() { 
     var myID = "#" + (this).attr("id"); 
     var id = $(this).attr("value"); 
     $.ajax({ 
      type: "POST", 
      url: "ajax/blogPublishUnpublish.php", 
      data: "id="+ id , 
      success:function(result){ 

       if (result == '0' || result == '1'){ 
        alert("in"); 
        $(myID).toggleClass("red"); 

        if($(myID).is('.green')) 
         $(myID).text('publish'); 

        if($(myID).is('.red')) 
         $(myID).text('unpublish'); 
        alert("done"); 
       } 

      } 
     }); 
     // alert(index); 


    }); 
    }); 
+0

我沒有設法做到這一點。 我不得不嘗試別的東西。 Thanx – 2012-03-02 12:57:31

+0

究竟發生了什麼?什麼是失敗? – tedski 2012-03-02 15:26:28

0

因爲我沒有設法做到這一點,所以我稍微改變了它。

我的按鈕:

<button id="publish" 
        <?php 
         if ($singleBlog->isPublished()) 
          echo ' class="green small"'; 
         else 
          echo ' class="red small"'; 
        ?> 
        value="<?php echo $singleBlog->getBlogId(); ?>"><?php 
         if ($singleBlog->isPublished()) 
          echo 'published'; 
         else 
          echo 'unpublished'; 
        ?></button><img id="progress<?php echo $singleBlog->getBlogId(); ?>" src="includes/images/progress.gif" style="display:none" /></td> 

和M阿賈克斯

$(document).ready(function() { 
      $("button#publish").click(function() { 
       var id = $(this).attr("value"); 
       var progress = '#'+$(this).attr('progress'+id); 
       $('#progress'+id).show(); 
       if ($(this).text() == 'published') 
        $(this).text('unpublished'); 
       else 
        $(this).text('published'); 
       $(this).toggleClass('green'); 
       $(this).toggleClass('red'); 
       $.ajax({ 
         type: "POST", 
         url: "ajax/blogPublishUnpublish.php", 
         data: "id="+ id , 
         success:function(result){ 
          if (result == -1){ 
           // add X button in case if fails ! 
          } 
          $('#progress'+id).hide(); 
        } 
       }); 
     }); 
     }); 

不是最優化的解決方案,但它是我能想出一個頁面來處理多個實例的唯一途徑。