重置表单(把表单的所有输入元素重置为它们的默认值。):
1.使用reset按钮,条件reset按钮必须在form表单内部。
2. <input id="Button1" type="button" value="button" onclick="form1.reset();" />可以不在表单内部。
用途示例:一般我们做添加页面和编辑页面时用的都是同一个页面,这样以来编辑后再添加时表单内容就需要清除,很多人在使用后台代码做清除工作如:
protected void btnAdd_Click(object sender, EventArgs e)
{
lblCaption.Text = "添加";
ArticleType1.ddlTypeEnabled = false;
ArticleNoImg.txtTitleText = "";
ArticleNoImg.fckContentText = "";
ArticleType1.ddlTypeSelectValue = articleTypeId;
lblUpFileName.Visible = false;
lblDocFileName.Visible = false;
lblNotice.Visible = false;
imgBtnDelete.Visible = false;
......
SelectedId = "";
ModalPopup.Show();
}
实际上我们可以更简单些,使用js脚本来实现:
<asp:Button ID="btnAdd" runat="server" Text="添 加" CausesValidation="False" OnClientClick="form1.reset();ModalPopup.style.display='';" />
或者
<input id="Button1" type="button" value="button" onclick="form1.reset();ModalPopup.style.display='';" />
另外:
有些项(下拉框、复选列表、单选列表)我们只要有默认值,reset并不会把它们清除掉,而是自动设置成默认项。
<asp:TextBox ID="TextBox1" runat="server">aaa</asp:TextBox>
<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem>aaa</asp:ListItem>
<asp:ListItem Selected="True">bbb</asp:ListItem>
<asp:ListItem Selected="True">ccc</asp:ListItem>
</asp:CheckBoxList>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Selected="True">aa</asp:ListItem>
<asp:ListItem>bb</asp:ListItem>
<asp:ListItem>cc</asp:ListItem>
</asp:RadioButtonList>
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem>aa</asp:ListItem>
<asp:ListItem>bb</asp:ListItem>
<asp:ListItem Selected="True">cc</asp:ListItem>
</asp:DropDownList>
<asp:ListBox ID="ListBox1" runat="server">
<asp:ListItem>a</asp:ListItem>
<asp:ListItem Selected="True">b</asp:ListItem>
<asp:ListItem>c</asp:ListItem>
</asp:ListBox>
onsubmit,onreset的用法:
<script type="javascript">
function check(theform)
{
这里写你要检查一些输入是否合法
如果合法就提交表单,去执行你的下个页面(uurl)
如果不合法就return false;这样就不提交页面
}
function tips(theform)
{
表单重置会清空当前内容,您确定要重置表单吗?
}
</script>
<form name="form1" action="uurl" onsubmit="return(check(this));" onreset="return tips(this);">
....
....
</form>
reture false;表示该事件完成之后,不再交给IE默认处理了,
一般提交按钮的动作是这样:
点击按钮->OnSubmit() ------ return true -> submit()
|
|--->return false 就不往下执行submit()了。