首页 > 代码库 > 清空所有textbox
清空所有textbox
//后台清空,刷新页面,不推荐
foreach( Control childControl in this.Controls )
{
if( childControl is TextBox )
((TextBox)childControl).Text = "";
}
}
foreach( Control childControl in this.Controls )
{
if( childControl is TextBox )
((TextBox)childControl).Text = "";
}
}
//用反射:
FieldInfo[] infos = GetType().GetFields( BindingFlags.Public | BindingFlags.NonPublic|BindingFlags.GetField |BindingFlags.Instance);
for(int i=0;i<infos.Length;i++)
{
if(infos[i].FieldType == typeof(TextBox))
{
((TextBox)infos[i].GetValue(this)).Text = "";
}
}
//使用js,好处是不用刷新页面:
function ClearAllTextBox()
{
var obj=window.document.forms[0];
for(i=0;i<obj.elements.length; i++)
{
var elem=obj.elements[i];
if(elem)
{
if(elem.type=="text")
{
elem.value="";
}
}
}
}
//最后一种用Jquery
function ClearText() {
$("input").each(function() {
$(this).val(‘‘);
});
}
清空所有textbox