如何在文本区域中设置空值

时间:2013-04-15 05:04:29

标签: c# javascript asp.net

我想在textarea的onclick()事件中设置null值,但我的代码无法正常工作..

<textarea id="txtwishlistsong1" rows="5" cols="70" runat="server" onclick="return txtwishlistsong1_onclick()">Type Your Message Here...</textarea>

背后的代码

function txtwishlistsong1_onclick()
{
   document.getElementById('txtwishlistsong1').focus();
   document.getElementById('txtwishlistsong1').value=null;
}

3 个答案:

答案 0 :(得分:5)

您将控件定义为runat="server",以便在JavaScript中访问它们,您需要指定ClientID。

function txtwishlistsong1_onclick()
{
   document.getElementById('<%= txtwishlistsong1.ClientID %>').focus();
   document.getElementById('<%= txtwishlistsong1.ClientID %>').value='';
}

或者,如果您使用的是.Net framework 4.0或更高版本,则可以为控件指定ClientIDMode = "Static",详细了解ClientIDMode

答案 1 :(得分:1)

设置空值:""(不是null)。

根据此source,值为DOMString,且必须始终具有input的当前值。由于input实际上不具有null值(在这种情况下它相当抽象),因此空值是更好的选项(并且在浏览器中一致地工作)。

答案 2 :(得分:0)

可能原因是asp.net ID错误。

尝试使用:

document.getElementById('<%=txtwishlistsong1.ClientID%>').focus();
document.getElementById('<%=txtwishlistsong1.ClientID%>').value=null;
相关问题