2010-04-04 57 views
0

我試圖在我的項目中提出請求/回覆部分。
我想在代碼中實現這些功能(我無法實現;所以,請大家幫我解決):
1>當用戶點擊回覆按鈕時;其他回覆區域(文本區域+按鈕)應該隱藏(意味着一次只有一個回覆區域對用戶可見)。
2>當用戶點擊回覆按鈕時,文本區域將關注並且頁面將向下滑動(假設用戶回覆10條評論焦點將自動設置爲10號文本區域,頁面將相應地滑向該位置)。JQuery幫助,如何隱藏JQuery中的所有按鈕

這裏是我到目前爲止的代碼的傢伙:

//method call on the click of reply link. 
    function linkReply_Clicked(issueId) {  

     Id = issueId; 
     textId = "text_" + issueId + count; 
     btnReply = "btnReply_" + issueId + count; 
     btnCancel = "btnCancel_" + issueId + count; 

     var textareasArray = document.getElementsByTagName("textarea"); 
     var btnArray = document.getElementsByTagName("input"); 

     for (i = 0; i < textareasArray.length; i++) { 
      textareasArray[i].style.display = "none"; 
      btnArray[i].style.display = "none"; 
     } 

     var str = "<table cellpadding='3' cellspacing='0' width='58%'>"; 
     str += "<tr><td valign='top' align='left'>"; 
     str += "<textarea id=" + textId + " rows='5' cols='60'></textarea>"; 
     str += "</td></tr>"; 
     str += "<tr><td valign='top' align='right'>"; 
     str += "<input id=" + btnReply + " type='button' onclick='btnReply_Clicked(Id ,textId)' value='Reply' />&nbsp;"; 
     str += "<input id=" + btnCancel + " type='button' onclick='btnCancel_Clicked(Id ,textId)' value='Cancel' />&nbsp;"; 
     str += "</td></tr>"; 
     str += "</table>"; 
     document.getElementById("divOuter_" + issueId).innerHTML = str; 
     $("#" + textId + "").focus(); 

    } 
    // submit user reply and try to hide that reply area. 
    function btnReply_Clicked(issueId, textID) { 

     var comment = document.getElementById(textID).value; 
     if (comment != '') { 
      $.getJSON("/Issue/SaveComment", { IssueId: issueId, Comment: comment }, null); 

      $("#text_" + issueId + count).hide(); 
      $("#btnReply_" + issueId + count).hide(); 
      $("#btnCancel_" + issueId + count).hide(); 
      document.getElementById(textID).value = ''; 
      count = count + 1; 
     } 
    } 

    // cancel user reply and try to hide that reply area. 
    function btnCancel_Clicked(issueId, textId) { 

     $("#text_" + issueId + count).hide(); 
     $("#btnReply_" + issueId + count).hide(); 
     $("#btnCancel_" + issueId + count).hide(); 
     document.getElementById(textId).value = ''; 
     count = count + 1; 
    } 
+0

這可能有助於看到(的一部分),你的HTML的結構爲好。 – 2010-04-04 12:10:34

回答

2

我改了一下這一點,因爲你可以因爲你已經使用jQuery :) Go here for the demo version

可以更換做到這一點很容易所有的代碼發佈此:

function linkReply_Clicked(issueId) { 
    $(".replyTable").hide();   
    var tbl = $("<table class='replyTable' cellpadding='3' cellspacing='0' width='58%'></table>"); 
    tbl.append("<tr><td valign='top' align='left'><textarea rows='5' cols='60'></textarea></td></tr>"); 
    tbl.append("<tr><td valign='top' align='right'><input type='button' class='reply' value='Reply' />&nbsp;<input type='button' class='cancel' value='Cancel' />&nbsp;</td></tr>"); 

    tbl.find(".reply").click(function() {    
     var comment = $(this).closest("table").find("textarea").val(); 
     if (comment != '') { 
      $.getJSON("/Issue/SaveComment", { IssueId: issueId, Comment: comment }, null); 
      $(this).closest("div").empty(); 
     } 
    }).siblings(".cancel").click(function() { 
     $(this).closest("div").empty(); 
    }); 

    var div = $("#divOuter_" + issueId).empty().append(tbl); 
    $('html, body').animate({ scrollTop: $(div).offset().top }, 500, 
          function() { div.find("textarea").focus(); }); 
} 

這做以下的事情用不同的幻燈片效果&隱藏和問題滾動:

  • 答覆點擊處理器/取消現在按鈕在線,無需額外的功能
  • 的輸入不再有你需要跟蹤的ID,它只是發現它們相對
  • 表中有一類replyTable所以它所有的舊的可快速隱藏
  • 輸入上課找到它們更容易(綁定的點擊處理程序)
  • 沒有更多count,無需:)
  • 動畫展示身體,做一個快速的滾動效應的位置和重點文本區域
  • 刪除舊錶清理
+0

對於一個問題很少的問題,這是很多工作=)+1 – 2010-04-04 12:21:42

+0

非常感謝尼克 – 2010-04-05 05:44:56