我有一个Validate函数,它根据插入的值返回true和false。我想验证并禁用提交按钮。 for ex OnClientClick =“return Validate(this);”如果返回true,则提交到数据库并在该段时间内禁用提交,如果为false,则禁用提交和警报并显示验证消息。
<script type="text/javascript">
function Validate(b) {
function stuff() {
var temp = document.getElementById("<%=txt_stuff.ClientID %>").value;
var val = /^[a-zA-Z0-9 ]+$/
if (temp == "") {
alert("Please Enter Stuff");
return false;
} else if (val.test(temp)) {
return true;
} else {
alert("Name accepts only spaces and charcters");
return false;
}
}
function price() {
var temp2 = document.getElementById("<%=txt_price.ClientID %>").value;
var val2 = /^[0-9 ]+$/
if (temp2 == "") {
alert("Please Enter Price");
return false;
} else if (val2.test(temp2)) {
return true;
} else {
alert("Price accepts only Number");
return false;
}
}
if (stuff() && price()) {
b.disabled = true;
b.value = 'Submitting...';
return true;
} else {
return false;
}
}
</script>
这是按钮代码
<asp:Button ID="Button2" runat="server" Text="Add Record" OnClientClick = "return Validate(this)"
onclick="Button2_Click" />
该按钮被禁用但该值未提交到数据库中,并且该按钮也不会再次启用。可能是什么原因?
数据库代码
protected void Button2_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(500);
SqlConnection con = new SqlConnection();
con.ConnectionString = "Data Source=DTPXP-77A;Initial Catalog=practice;Integrated Security=true";
con.Open();
SqlCommand cmd = new SqlCommand("insert into expense values(@person,@item,@expdate,@price)", con);
cmd.Parameters.Add("@person", SqlDbType.VarChar);
cmd.Parameters.Add("@item", SqlDbType.VarChar);
cmd.Parameters.Add("@expdate", SqlDbType.DateTime);
cmd.Parameters.Add("@price", SqlDbType.Int);
cmd.Parameters["@person"].Value = droplist_person.SelectedItem.ToString();
cmd.Parameters["@item"].Value = txt_stuff.Text;
cmd.Parameters["@expdate"].Value = DateTime.Now.ToString();
cmd.Parameters["@price"].Value = txt_price.Text;
cmd.ExecuteNonQuery();
InsertHistory();
Response.Redirect("Add.aspx");
}