2012-03-01 11 views
0

私は複数の公開および非公開のボタンがあるテーブルを持っています。JQuery Parent from ajax

シンプルなボタン:

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

+0

何が起こっているのですか?何が失敗していますか? – tedski

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> 

とメートルAJAX

$(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(); 
        } 
       }); 
     }); 
     }); 

は、最も最適なソリューションが、それは私は1つのページに複数のインスタンスを処理するために把握できる唯一の方法でした。