2012-08-16 137 views
1

誰能告訴我爲什麼我的代碼:$(this).parent().hide();作品(選擇DIV是隱藏的),當放入我的.post()像外界:jQuery的隱藏不工作.post的內部函數成功

$(document).on('submit', '.reply-message-form', function(e) { 

     $(this).parent().hide(); 

     if($(this).children('.post-reply-message-textarea').val() == '') 
      return false; 

     $.post("<?php echo Yii::app()->createUrl('event/view', array('id'=>Yii::app()->controller->actionParams['id'])); ?>", 
     $(this).serialize(), function(response) { 

      var responseObject = jQuery.parseJSON(response); 
      // if successful.. process.. 
      if (responseObject.success == true) { 

      } else { 
       alert('failed'); 
      } 

     }); 
     return false; 
    }) ; 

但是如果.hide()裏面.post()成功功能沒事發生..?!當內部代碼:

$(document).on('submit', '.reply-message-form', function(e) { 

     if($(this).children('.post-reply-message-textarea').val() == '') 
      return false; 

     $.post("<?php echo Yii::app()->createUrl('event/view', array('id'=>Yii::app()->controller->actionParams['id'])); ?>", 
     $(this).serialize(), function(response) { 

      $(this).parent().hide(); 
      return false; 
      var responseObject = jQuery.parseJSON(response); 
      // if successful.. process.. 
      if (responseObject.success == true) { 

      } else { 
       alert('failed'); 
      } 

     }); 
     return false; 
    }) ; 

只是爲了澄清任何疑問 - 在responseObject.success不== TRUE(我已經證實,與報警等)。

在此先感謝!

回答

4

請嘗試,因爲$(this)在後期回調中移動時不相同。

$(document).on('submit', '.reply-message-form', function(e) { 
    var elt = $(this).parent(); 
    if($(this).children('.post-reply-message-textarea').val() == '') 
     return false; 

    $.post("<?php echo Yii::app()->createUrl('event/view', array('id'=>Yii::app()->controller->actionParams['id'])); ?>", 
    $(this).serialize(), function(response) { 

     $(elt).hide(); 
     return false; 
     var responseObject = jQuery.parseJSON(response); 
     // if successful.. process.. 
     if (responseObject.success == true) { 

     } else { 
      alert('failed'); 
     } 

    }); 
    return false; 
}) ; 
+0

你打我。我喜歡這種方式,因爲所有你需要的是成功回調中的父項。 – Gromer 2012-08-16 17:34:17

+1

是有道理的 - 謝謝! – 2012-08-16 17:34:24

1

this並不代表提交的表單了,因爲它是從Ajax調用回調。在進行ajax調用之前,用此設置一個變量。

var myForm = this; 

然後在你的成功回調,參考$(myForm)