我有一个 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");
}