2011-10-20 30 views
0

讓我分享我的jQuery部分首先

$(".btnUpdateInput").click(function() { 
      //alert("Update"); 
      var BldDocumentId = $("#BldDocId").val(); 

      var BldinstructionId = $("#BldIPInstnId").val(); 
      var InputFieldId = $("#InputFieldId").val(); 
      var InputFieldValue = jQuery.trim($(this).parent().prev().text()); //$("#InputFieldValue").val(); 
      var InputFieldUserValue = jQuery.trim($(this).prev().val()); // $(".user_input_value").val(); 
      alert(BldDocumentId + "," + BldinstructionId + "," + InputFieldId + "," + InputFieldValue + "," + InputFieldUserValue); 

      var postResult; 
      alert($(this).get()); 
      $.post("/Build/UpdateInputField", 
       { bldDocId: BldDocumentId, bldInstnId: BldinstructionId, inputFieldId: InputFieldId, inputFieldValue: InputFieldValue, inputFieldUserValue: InputFieldUserValue }, 
       function(result) { 
       postResult = result; 
        //Either this should function** 
        alert($(this).get()); // returned Object[Object] but expected the clicked button 
        alert($(this).parent().get());// returned null 
        alert($(this).parent().next().get());//returned null 
        alert($(this).parent().next().children().first().get());// returned null 

        // $(this).parent().next().show(); 
        // $(this).parent().next().children().first().text(InputFieldUserValue); 
        // $(this).parent().hide(); 
       }); 

       alert(postResult); 
      //Or this should function** 
      if (postResult == true) { 
       $(this).parent().next().show(); 
       $(this).parent().next().children().first().text(InputFieldUserValue); 
       $(this).parent().hide(); 
      } 
     }); 

現在讓我解釋一下我的問題。我需要顯示並隱藏少數divs相對於我點擊的按鈕「btnUpdateInput」。我嘗試了兩種方式:1,我給下面的線路中的在$。員額

$.post("/Build/UpdateInputField", 
       { bldDocId: BldDocumentId, bldInstnId: BldinstructionId, inputFieldId: InputFieldId, inputFieldValue: InputFieldValue, inputFieldUserValue: InputFieldUserValue }, 
       function(result) { 
       postResult = result; 
        //Either this should function** 
        alert($(this).get()); // returned Object[Object] but expected the clicked button 
        alert($(this).parent().get());// returned null 
        alert($(this).parent().next().get());//returned null 
        alert($(this).parent().next().children().first().get());// returned null 

        // $(this).parent().next().show(); 
        // $(this).parent().next().children().first().text(InputFieldUserValue); 
        // $(this).parent().hide(); 
       }); 

2.成功的部分或得到postResult的價值出來,比較和做同樣存在。代碼如下:

if (postResult == true) { 
       $(this).parent().next().show(); 
       $(this).parent().next().children().first().text(InputFieldUserValue); 
       $(this).parent().hide(); 
      } 

兩者都不適合我。在1上午沒有得到$(this)作爲我單擊的按鈕'btnUpdateInput',並且2. postResult的值是未定義的,因爲沒有延遲,因此postResult被分配給了post操作的結果。

請幫我解決這兩種情況。

+0

這將是如果您也發佈了相關的HTML源代碼,這會更有幫助。 – reporter

+0

你真的想要什麼$(this)? – rhapsodyn

回答

1

這是您當前IN中的元素。所以在SUCCES它的回報。

所以一個方法,你可以這樣做:

$(".btnUpdateInput").click(function(e) { 
var self = e; 
//code 
alert($(self).get()); // returned Object[Object] but expected the clicked button 
alert($(self).parent().get());// returned null 
alert($(self).parent().next().get());//returned null 
alert($(self).parent().next().children().first().get());// returned null 
} 

似乎是不言也被用來作爲默認....

試試這個:

$(".btnUpdateInput").click(function(e) { 
var myButton = $(this); 
//code 
alert($(myButton).get()); // returned Object[Object] but expected the clicked button 
alert($(myButton).parent().get());// returned null 
alert($(myButton).parent().next().get());//returned null 
alert($(myButton).parent().next().children().first().get());// returned null 
} 
+0

仍然得到與先前相同的結果:(除第一個以外,所有的警報返回null –

+0

更新後:) –

+0

謝謝...它的工作原理 –