2012-08-12 42 views
0
function clickButton(e, buttonid)   
      var evt = e ? e : window.event; 
      var bt = document.getElementById(buttonid); 
      if (bt) { 
       if (evt.keyCode == 13) { 
        bt.click();             
        return false; 
       } 
      } 
     } 

txtChatMessage.Attributes.Add("onkeypress", "return clickButton(event,'" + btnSendChat.ClientID + "')"); 

此功能是在代码隐藏文件中设置的属性。如何重置在文本框中的文本按钮点击火灾后通过javascript重置文本框文本

回答

0

你可以像这样做,从代码传递文本字段后面using thissettingvalue to empty string在JavaScript

在后面的代码

txtChatMessage.Attributes.Add("onkeypress", "return clickButton(this, event,'" + btnSendChat.ClientID + "')"); 

在javascript中

function clickButton(txt, e, buttonid){ 
    var evt = e ? e : window.event; 
    var bt = document.getElementById(buttonid); 
    if (bt) { 
     if (evt.keyCode == 13) { 
      bt.click();  
      txt.value = "";            
      return false; 
    } 

上面的代码会覆盖现有的文本框的值。要保存以备后用,我们可以使用隐藏域

在HTML

<asp:hidden id="hdnText" runat="server" > 

在javascript中

function clickButton(txt, e, buttonid){ 
    var evt = e ? e : window.event; 
    var bt = document.getElementById(buttonid); 
    if (bt) { 
     if (evt.keyCode == 13) { 
      bt.click();  
      document.getElementById('<%= hdnText.ClientID %>').value = txt.value;            
      return false; 
    } 

在后面的代码

发送文本字段到JavaScript的乐趣从隐藏字段的文本框的ction

txtChatMessage.Attributes.Add("onkeypress", "return clickButton(this, event,'" + btnSendChat.ClientID + "')"); 

获得价值

string textBoxValue = hdnText.Value; 
+0

我已经尝试设置通过jQuery的价值,但这样做在Mozilla Firefox中一个有趣的现象。文本消息不通过服务器,但空白的消息去服务器 – 2012-08-12 11:36:51

+0

这是它应该怎么做,你不重置在JavaScript,但在服务器上然后。 – Adil 2012-08-12 11:38:32

+0

我已经使用隐藏字段更新了我的答案,以保留文本字段的值,我希望这会对您有所帮助。 – Adil 2012-08-12 11:54:27