一,form的onsubmit button的onclick事件
<html>
<head>
<title>a test for submit</title>
<script language="javascript">
function test1()
{
alert("this form will not be submitted!");
return true;//如果是false则form 的onsubmit事件将不被执行了
}
function test()
{
alert("submitform");//如果是false执行也将中止
}
//提交执行顺序是先submit的onclick,然后是form的onsubmit事件
</script>
</head>
<body>
<form id="form1" onsubmit="return test()" action="Default.aspx">
<input type="submit" onclick="return test1()" />
</form>
</body>
</html>
二给button加onclick事件
<html>
<head runat="server">
<title>test button</title>
<script language="javascript" type="text/javascript">
function checkUser()
{
var user=$("txtName").value;
if(user.length==0&&user=="")
{
alert("you didn't input anything!");
user.value="";
return false;
}
return true;
}
function $(id)
{
return document.getElementById(id);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtName" runat="server" EnableViewState="False"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" EnableViewState="False" /></div>
</form>
</body>
</html>
后台代码
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Button1.Attributes.Add("onclick", "return checkUser()");
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("this is a good day!");
}
}