我有两个文本框和启用按钮。如果在单击启用按钮的文本框中错误地输入数据,则会提醒它。如果所有数据都正确,则单击启用按钮时,文本必须禁用。问题是,一旦我点击启用它变为禁用一秒然后页面再次加载它去启用。任何人都告诉我我做错了。请尽可能提供一个例子。谢谢提前
验证和更改按钮文本的脚本
function validate
{
var cal1 = document.getElementById('<%=txtEndDate.ClientId%>').value;
var cal2 = document.getElementById('<%=txtStartDate.ClientId%>').value;
var button=document.getElementById('<%=button1.ClientId %>')
if (cal1 == '' && cal2 == '') {
alert("Start Date and End Date can not be left blank ");
return false;
}
if (cal1 == '') {
alert("End Date can not be left blank ");
return false;
}
if (cal2 == '') {
alert("Start Date can not be left blank ");
return false;
}
if (cal1 != '' || cal2 != '')
{
var dt1 = Date.parse(cal1);
var dt2 = Date.parse(cal2);
if (dt1 <= dt2) {
alert("End Date must occur after the Start date ");
return false;
}
}
//if the all the validation are correct it comes to this
if (button.value == "Enable")
button.value = "Disable";
else
button.value = "Enable";
return true;
}
按钮
<asp:Button ID="button1" OnClientClick=" return validate()" runat="server" Text="Enable" />
答案 0 :(得分:2)
您的问题是按钮(<input type="button">
)的点击事件的默认操作是提交表单。您可以通过从事件处理程序返回false
来阻止这种情况。在您的情况下,最简单的方法是从验证函数中始终return false
。
答案 1 :(得分:2)
条件将&&
代替||
,您通过return false
来停止回发
完成验证后,尝试return false
并使用if - else
将节省validate
的时间
function validate()
{
var cal1 = document.getElementById('<%=txtEndDate.ClientId%>').value;
var cal2 = document.getElementById('<%=txtStartDate.ClientId%>').value;
var button=document.getElementById('<%=button1.ClientId %>')
if (cal1 == '') {
alert("End Date can not be left blank ");
return false;
}
else if (cal2 == '') {
alert("Start Date can not be left blank ");
return false;
}
else if (cal1 != '' && cal2 != '')
{
var dt1 = Date.parse(cal1);
var dt2 = Date.parse(cal2);
if (dt1 <= dt2) {
alert("End Date must occur after the Start date ");
return false;
}
else{
//if the all the validation are correct it comes to this
if (button.value == "Enable")
button.value = "Disable";
else
button.value = "Enable";
}
}
return false;
}
和按钮 - <asp:Button ID="button1" OnClientClick=" return validate();" runat="server" Text="Enable" />
答案 2 :(得分:0)
完成验证后,尝试返回false
diff file1 file2 | awk '{print $2}' | grep -v '^$' > output.file