你可以像這樣做,從代碼傳遞文本字段後面using this
和setting
其value 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;
我已經嘗試設置通過jQuery的價值,但這樣做在Mozilla Firefox中一個有趣的現象。文本消息不通過服務器,但空白的消息去服務器 – 2012-08-12 11:36:51
這是它應該怎麼做,你不重置在JavaScript,但在服務器上然後。 – Adil 2012-08-12 11:38:32
我已經使用隱藏字段更新了我的答案,以保留文本字段的值,我希望這會對您有所幫助。 – Adil 2012-08-12 11:54:27