2011-11-12 21 views
0

所以我有以下Javascript。我會試着找出爲什麼CommentItem DIV我追加內指定的文本沒有顯示。但我的主要問題是靶向ContainerPh發起的一切txtComment那個那上面......不能針對特定的div來從jQuery AJAX填充數據ASP.NET

$('.txtComment').live("keypress", function (event) { 
$_this = $(this); 

var Id = $(this).attr("index"); 
var d = JSON.stringify({ "Value": $(this).val(), "StreamId": Id }) 
var keycode = (event.keyCode ? event.keyCode : event.which); 
if (keycode == '13') { 
    event.preventDefault(); 
    $.ajax({ 
     type: 'POST', 
     url: '../API/Services.asmx/PostComment', 
     data: d, 
     contentType: 'application/json; charset=utf-8', 
     dataType: 'json', 
     success: PostOk, 
     error: errFunc 
    }); 
    $('.txtComment').each(function() { 
     $(this).attr('disabled', 'disabled'); 
    }); 
} 

}); 

function PostOk(result, userContext, methodName) { 

var CommentsPh = $_this.parent().parent().find(".CommentsPh"); 
var CommentItem = 
    "<div class='CommentItem'>" + 
     "<div id='CommentImgContainer'>" + 
     "<img src='' class='CommentImg' + '.thumb' />" 
     "</div>" + 
     "<div id='CommentStory'>" + 
      "<a href='' class='BuddyTag small'></a>" + 
      "<span class='CommentValue'>" + result.d[1] + "</span>" + 
      "<br />" + 
      "<div class='spacer'></div>" + 
      "<span class='DateTime'>datetime</span>" 
      "<span style='float: right; margin-right: 7px; margin-left:8px; line-height:8pt;'>.</span>" + 
      "<span class='CommentUp'>sometext</span>" + 
      "<span class='CommentLike'></span>" + 
     "</div>" + 
    "</div>"; 

    $(CommentsPh).append(CommentItem); 

$('.txtComment').each(function() { 
    $(this).removeAttr('disabled'); 
    }); 
} 

這正在填充所有.CommentsPh即使我想在txtComment文本框之前獲得一個。我究竟做錯了什麼?

       <asp:Button ID="btnDeleteStream" CssClass="DisposeOfStream" OnClick="btnDeleteStream_Click" 
            runat="server" /> 
          </div> 
         </asp:Panel> 





           <div class="CommentsPh"> 
           </div> 
           <asp:Button ID="btnDeleteStream" CssClass="DisposeOfStream" OnClick="btnDeleteStream_Click" 
            runat="server" /> 
          </div> 
         </asp:Panel> 

         <asp:Panel ID="CommentTools" CssClass="CommentTools" runat="server"> 
          <asp:TextBox ID="txtComment" TextMode="SingleLine" CssClass="txtComment" runat="server"></asp:TextBox> 

想法?謝謝。

回答

2

我們看不到很多標記,所以很難說這個TextBox上面的兩個父母沒有多個.CommensPh div(哪個#find會匹配)。

作爲替代方案,是否有任何理由不把id =屬性放在所需的div標籤上,使用一些將TextBox綁定到該div的約定?我想是這樣......

<div id='comments_txtComment'></div> 

...這裏所討論的文本框是...

<asp:TextBox ID='txtComment'> 

...然後在PostOK,這樣做VAR CommentsPh = $ ( '評論_' + $ _ this.attr( '身份證'));

+2

我不能在其他答覆註釋搜索,所以我會在這裏做...他們一直說$ _this變量是在該按鍵處理程序中聲明的。情況並非如此......它只是在它內部進行分配。我們沒有看到足夠的代碼來知道它的定義。無論如何,如果你沒有用'var'聲明變量,我相信它們是全局的。 – dlgrasse

+0

mmm可擴展範圍問題正如你所說,但你不能像這樣使用var CommentsPh = $ _this.parent()。parent()。find(「。CommentsPh」)。因爲你在使用$ _this。你必須使用像我說 –

+0

@digrasse的屬性,你是對的。 – Pointy

1

,因爲您指定爲

$_this = $(this); 

$_this shoul be id or class or other attribute but not the object 

您可以像使用

$_this = this.id; 
var CommentsPh = $('#'+$_this).parent().parent().find(".CommentsPh"); 

你不能使用像

var CommentsPh = $_this.parent().parent().find(".CommentsPh"); 

的也是變量的作用域是不正確

$_this is defined in side 

$('.txtComment').live("keypress", function (event) { 

所以它只能在該函數中使用

聲明出來端的任何功能,但裏面

$(document).ready 

$(document).ready(function(){ 

var $_this = ''; 

}); 
1

指定Id tagdiv要在DOM找人。 然後搜索。

<div id="comment" class="CommentsPh"> 
            hhvghg 
           </div> 

然後根據自己的ID標籤或者其ID changes.Use此代碼段找到DIV

$('div[id*=comment]').hide();