2013-03-04 97 views
1

我在我的論壇上進行引用功能。
當用戶點擊Quote鏈接時,我想將此帖內容添加到文本框(txtPost) - 內容不是問題(我試過alert(content),它工作正常)。
但文本框中的文本不刷新 - alert(txtPost.value)顯示一些添加的內容等,但文本框仍然是空的。爲什麼?如何解決它?通過javascript將文本添加到文本框中

帖子:

<asp:HyperLink ID="quotebutton" CssClass="quotebutton" OnClick="javascript:addQuote('somecontent');" runat="server">Quote</asp:HyperLink> 

文本框代碼:

 <nforum:Emoticons ID="emoticonInclude" runat="server" /> 
     <div style="width: 604px; margin-left: 198px;"> 
      <asp:TextBox ID="txtPost" runat="server" TextMode="MultiLine" Rows="14" Width="600" 
       ClientIDMode="Static"></asp:TextBox> 
     </div> 

JavaScript函數:

function addQuote(content) { 
    var txtPost = document.getElementById("txtPost"); 
    alert(content); 
    txtPost.value = txtPost.value + content; 
    alert(txtPost.value); 
} 

信息!編輯

我正在使用tinyMCE編輯器。仍然不刷新內容,而tinyMCE連接到此文本框。怎麼做?

<script type="text/javascript"> 
    tinyMCE.init({ 
     // General options 
     mode: "exact", 
     elements: "txtPost", 
     theme: "advanced", 
     plugins: "insertcode", 
     // Theme options 
     theme_advanced_buttons1: "bold,italic,underline,strikethrough,|,formatselect,|,bullist,numlist,|,link,unlink,insertcode", 
     theme_advanced_buttons2 : "", 
     theme_advanced_buttons3 : "", 
     theme_advanced_toolbar_location: "top", 
     theme_advanced_toolbar_align: "center", 
     theme_advanced_resizing: true, 
     remove_linebreaks: false, 
     relative_urls: false, 
     content_css: "/css/nforumeditor.css" 
    }); 

</script> 

問題解決了

對於tinyMCE-

function addQuote(content) { 
    tinyMCE.execCommand('mceInsertContent', false, content); 
    return false; 
} 

回答

0

頭應該在功能return false;,防止回發。

0

在JavaScript中使用,你必須調用/獲取服務器控件的客戶端ID

HTML

<asp:HyperLink ID="quotebutton" CssClass="quotebutton" OnClick="javascript:return addQuote('somecontent');" runat="server">Quote</asp:HyperLink> 

的JavaScript

function addQuote(content) { 
var txtPost = document.getElementById("<%= txtPost.ClientID %>"); 
txtPost.value = txtPost.value + content; 
return false; 
} 
+0

阿迪爾和你們之間的服務器控件,你已經找到了解。您可以將Adil的解決方案添加到您的答案中,也可以將其添加到您的答案中,並且您中的任何一個都可以獲得+1。 :) – Bazzz 2013-03-04 09:45:55

0
<asp:TextBox ID="txtPost" runat="server"></asp:TextBox> 
<asp:HyperLink ID="quotebutton" runat="server" onclick="javascript:addQuote('some content');">Quote</asp:HyperLink> 

function addQuote(content) { 
     var txtPost = document.getElementById('<%= txtPost.ClientID %>'); 
     txtPost.value = txtPost.value + content; 
    }